gpt4 book ai didi

android - 有趣的小部件难题

转载 作者:行者123 更新时间:2023-11-29 02:02:25 26 4
gpt4 key购买 nike

我有一个小部件,它主要执行我希望它执行的操作(启动一个 Intent ),但我想知道如何将多个 Intent 压缩到一个小部件提供程序中。

我已经尝试了所有常用的方法来完成这项工作:

  • 使用多个 Intent
  • 使用具有相同主题设置的第二个小部件以及其他一些最终以失败告终的人。

通用代码:

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);
}
}

所以我想弄清楚如何在小部件上获得多个按钮。由于它是为三个人设置的,而且我知道其中一个可以工作,所以我已经完成了 1/3。

这是 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 个不同的 PendingIntents 和 3 个不同的 Intents,然后调用 setOnClickPendingIntent 3 次。每个 Action 和按钮一个。下面是一个示例,假设三个 Activity 是 myClass、myClass2 和 myClass3。使用以下代码,每个按钮都会触发不同的 Intent 。

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);
}

关于android - 有趣的小部件难题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11972646/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com