我有一个显示简单文本和3个按钮的小部件:
我已经完成了刷新按钮的设置,并且工作正常,但是我似乎无法找出一种方法来处理其他两个按钮
PS。
我不需要实际的代码,我已经知道如何进行复制和共享,我只需要知道如何处理单击事件到目前为止是我的代码:
Button copy_content;
Button share_content;
void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
/** Code below will be executed once the timer is over*/
String widgetText = RandQuotes[rand.nextInt(RandQuotes.length)];
// Construct the RemoteViews object
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.quotes_widget);
views.setTextViewText(R.id.sayings, widgetText);
// Instruct the widget manager to update the widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// There may be multiple widgets active, so update all of them
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
final int count = appWidgetIds.length;
for (int i = 0; i < count; i++) {
int widgetId = appWidgetIds[i];
String on_sayings = RandQuotes[rand.nextInt(RandQuotes.length)];
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.quotes_widget);
remoteViews.setTextViewText(R.id.sayings, on_sayings);
Intent intent = new Intent(context, HavamalWidget.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.switch_trigger, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
[我有一个显示简单文本和3个按钮的小部件:刷新(选择另一个随机字符串并显示它)复制(将textview的内容复制到剪贴板)共享(共享...的内容)]
只需通过操作使按钮上的待定意图。因此,在onReceive()中,只需检查意图动作并对此做一些逻辑即可。如果您要做一些长的任务,最好在IntentService中进行所有逻辑处理。
在像这样的RemoteViews中的按钮上设置待处理的意图: