Android 有趣的小部件难题
我有一个小部件,它主要做我想做的事情(启动一个意图),但我想知道如何将多个意图压缩到一个小部件提供者中 我已经尝试了所有通常的方法来完成这项工作:Android 有趣的小部件难题,android,android-intent,widget,Android,Android Intent,Widget,我有一个小部件,它主要做我想做的事情(启动一个意图),但我想知道如何将多个意图压缩到一个小部件提供者中 我已经尝试了所有通常的方法来完成这项工作: 使用多个意图 使用具有相同主题设置的第二个小部件 还有其他一些地方最终都失败了 通用代码: public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { for (int appWidgetId : appWid
- 使用多个意图
- 使用具有相同主题设置的第二个小部件 还有其他一些地方最终都失败了
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);
}
}
因此,我试图找出如何在小部件上获得多个按钮。因为它是为三个人设计的,而且我知道其中一个可以工作,所以我只有三分之一的成功率
以下是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>
好的,你就快拿到了!其思想是创建3个不同的
pendingent
和3个不同的intent
,然后调用setonclickpendingent
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);
}
发布xml文件R.layout的内容。MyWidget@edthethird很好的Bebop引用,XML已经添加。哈哈,它实际上是我的名字,没有意识到它是一个Bebop引用。。。不过很巧@第三次我肯定你知道那名字!除息的