2012-08-15 23 views
2

我有一個小部件,主要是做我想做的(開始一個意圖),但我想知道如何將多個意圖擠入一個小部件提供程序。有趣的小部件難題

我已經試過能完成這一操作所有常用的手段:

  • 使用一個以上的意圖
  • 用相同的主題設置 在這裏和那裏的幾個人說是最終第二控件故障。

通用代碼:

public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { 
    for (int appWidgetId : appWidgetIds) { 

     Intent startIntent = new Intent(context, myClass.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, startIntent, 0); 

     // Get the layout for the App Widget and attach an on-click listener to the button 
     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.MyWidget); 
     views.setOnClickPendingIntent(R.id.startBtn, pendingIntent); 

     // Tell the AppWidgetManager to perform an update on the current App Widget 
     appWidgetManager.updateAppWidget(appWidgetId, views); 
    }  
} 

所以我試圖找出如何獲得在widget更多然後一個按鈕。自從它成立三個,我知道其中一個作品,所以我在那裏的三分之一。

這裏是XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/startBtn" 
    android:layout_width="150dp" 
    android:layout_height="75dp" 
    android:layout_marginBottom="5dp" 
    android:background="@drawable/mybg" 
    android:gravity="center" 
    android:text="First Menu Item" 
    /> 

<TextView 
    android:id="@+id/startBtn2" 
    android:layout_width="150dp" 
    android:layout_height="75dp" 
    android:layout_marginBottom="5dp" 
    android:background="@drawable/mybg" 
    android:gravity="center" 
    android:text="Second menu item" /> 

<TextView 
    android:id="@+id/startBtn3" 
    android:layout_width="150dp" 
    android:layout_height="75dp" 
    android:layout_marginBottom="5dp" 
    android:background="@drawable/mybg" 
    android:gravity="center" 
    android:text="Third menu item" /> 

</LinearLayout> 
+0

發佈的XML文件的內容R.layout.MyWidget – edthethird 2012-08-15 17:13:57

+0

@edthethird星際很好的參考和XML已被添加。 – linuxrox 2012-08-15 17:22:29

+0

哈哈它實際上是我的名字,沒有意識到這是一個比波普參考...雖然不錯的巧合! – edthethird 2012-08-15 17:31:02

回答

1

OK,讓你幾乎得到它!這個想法是創建3個不同的PendingIntents和3個不同的Intents,然後撥打setOnClickPendingIntent 3次。每個動作和按鈕一個。這裏是一個例子,假設三個活動是myClass,myClass2和myClass3。用下面的代碼,每個按鈕都會觸發不同的意圖。

for (int appWidgetId : appWidgetIds) { 

    Intent startIntent = new Intent(context, myClass.class); 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, startIntent, 0); 

    Intent startIntent2 = new Intent(context, myClass2.class); 
    PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 0, startIntent2, 0); 

    Intent startIntent3 = new Intent(context, myClass3.class); 
    PendingIntent pendingIntent3 = PendingIntent.getActivity(context, 0, startIntent3, 0); 

    // Get the layout for the App Widget and attach an on-click listener to the button 
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.MyWidget); 
    views.setOnClickPendingIntent(R.id.startBtn, pendingIntent); 
    views.setOnClickPendingIntent(R.id.startBtn2, pendingIntent2); 
    views.setOnClickPendingIntent(R.id.startBtn3, pendingIntent3); 


    // Tell the AppWidgetManager to perform an update on the current App Widget 
    appWidgetManager.updateAppWidget(appWidgetId, views); 
} 
+0

太棒了!謝謝埃德! – linuxrox 2012-08-15 18:29:51