2012-05-16 42 views
0

我有一個小部件,我想在用戶單擊按鈕時更改圖像。我該怎麼做 ?進入RemoteView方法,我只能找到setOnClickPendingIntent ...我必須開始新的意圖,進入這個新的活動,我必須實現這個改變?Android:RemoteViews和setOnClickPendingIntent

由於

回答

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); 
    } 
相關問題