0

我有一个小部件,我想在用户单击按钮时更改图像。我该怎么做 ?进入 RemoteView 方法我只找到 setOnClickPendingIntent...我必须开始新的意图并进入这个新的活动我必须实现更改?

谢谢

4

1 回答 1

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);
  }
于 2012-05-16T07:37:54.023 回答