我有一个小部件,我想在用户点击按钮时更改图像。我该怎么做 ?进入RemoteView方法我只找到setOnClickPendingIntent ...我必须启动新意图并进入这个新活动我必须实现更改吗?
由于
答案 0 :(得分:0)
请参阅此示例:https://github.com/commonsguy/cw-advandroid/tree/master/AppWidget/PairOfDice
源代码在
之下 @Override
public void onUpdate(Context ctxt, AppWidgetManager mgr, int[] appWidgetIds) {
ComponentName me=new ComponentName(ctxt, AppWidget.class);
mgr.updateAppWidget(me, buildUpdate(ctxt, appWidgetIds));
}
private RemoteViews buildUpdate(Context ctxt, int[] appWidgetIds) {
RemoteViews updateViews=new RemoteViews(ctxt.getPackageName(), R.layout.widget);
Intent i=new Intent(ctxt, AppWidget.class);
i.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0 , i, PendingIntent.FLAG_UPDATE_CURRENT);
updateViews.setImageViewResource(R.id.left_die, IMAGES[(int)(Math.random()*6)]);
updateViews.setOnClickPendingIntent(R.id.left_die, pi);
updateViews.setImageViewResource(R.id.right_die, IMAGES[(int)(Math.random()*6)]);
updateViews.setOnClickPendingIntent(R.id.right_die, pi);
updateViews.setOnClickPendingIntent(R.id.background, pi);
return(updateViews);
}