Android App Widget - Button to change TextView content

核能气质少年 提交于 2019-12-19 11:06:09

问题


I'm a little confused with Android app widgets. I understand that I can create a button to start an activity, but I don't see how I can create a button to edit the text of a TextView inside the widget. Can someone please tell me what I'm missing?

Thanks a lot in advance.

EDIT: I don't mean for the user to input the new text, I mean for the application to change the text automatically, without any UI (i.e., dialog boxes or activities).


回答1:


EDIT: OK, so you set a getBroadcast PendingIntent on your button with setOnClickPendingIntent and perform your update (or call your update method) with the new text from the receiver. Your AppWidgetProvider class can be your receiver (you can catch your intent in onReceive), otherwise you create a new class that extends BroadcastReceiver.

EDIT2 (sample code):

I didn't run/compile this code so hopefully it will not have errors. This is a simple helper method to get a basic update pending intent with a custom action:

public PendingIntent getRefreshPendingIntent(Context context, int appWidgetId){
    Intent intent = new Intent("my.package.ACTION_UPDATE_WIDGET");
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

Catch the custom action in onReceive in your overridden AppWidgetProvider class

@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();

    //handle other appwidget actions    

    if (intent.getAction().equals("my.package.ACTION_UPDATE_WIDGET")) {
        //some code here that will update your widget
    }

    //handle other appwidget actions
}

You will also need to add an IntentFilter for the custom action to your manifest in the appwidget receiver declaration

<receiver
    android:name="my.package.MyWidgetProviderClass"
    android:label="MyWidget">
    <intent-filter>     
        <action android:name="my.package.ACTION_UPDATE_WIDGET"/>
        <!-- other intent filters -->
    </intent-filter>
    <!-- meta data pointing to widget xml file -->
</receiver>

Finally you apply the pending intent to your RemoteViews where ever you are building it so when the button is clicked it will send the my.package.ACTION_UPDATE_WIDGET action, which will be caught by your AppWidgetProvider, where you can (or call a method to) perform your AppWidget update

myRemoteViews.setOnClickPendingIntent(R.id.id_of_update_button, getRefreshPendingIntent(context, appWidgetId);



回答2:


This code updates the widget when you click on it , you can edit it and change the listener to your button :

public class MyWidgetProvider extends AppWidgetProvider {
private static final String ACTION_CLICK = "ACTION_CLICK";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
  int[] appWidgetIds) {

// Get all ids
ComponentName thisWidget = new ComponentName(context,
    MyWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {
  // Create some random data
  int number = (new Random().nextInt(100));

  RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
      R.layout.widget_layout);
  Log.w("WidgetExample", String.valueOf(number));
  // Set the text
  remoteViews.setTextViewText(R.id.update, String.valueOf(number));

  // Register an onClickListener
  Intent intent = new Intent(context, MyWidgetProvider.class);

  intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
  intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

  PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
      0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
  appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
} 

the complete tutorial is here.



来源:https://stackoverflow.com/questions/11907928/android-app-widget-button-to-change-textview-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!