gpt4 book ai didi

Android - 小部件点击监听器不起作用

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

我有 2 种可能的小部件布局,用户可以通过偏好 Activity 更改它们,我的意思是亮色和暗色。两者看起来很相似,onlu 颜色不同。事件 ID 是相同的。我想提供点击整个小部件的可能性,这将打开 Activity 并通过 Intent 发送必要的数据。我在 AppWidgetProvider 中的代码根本不起作用。

这里是:

private final String ACTION = "CLICK_MY_BUTTON";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
SharedPreferences theme = Data.getPreferences(context, Data.Prefs.THEME);
boolean dark = theme.getString(Data.widgetsThemeSetings, "1").equals("1");
int layoutId = dark?R.layout.widget_dark:R.layout.widget_light;
for (int i:appWidgetIds) {
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("from", "widget");
intent.setAction(ACTION);
int id = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
intent.putExtra("id", id<60?id:id-1);
intent.putExtra("widgetID", i);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), layoutId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.widget_relative_main, pendingIntent);
String output = "";
for (String s:HolidayCalendar.getInstance(context).getTodayTexts()) {
output += "\n\n" + Data.pointer + " " + s;
}
remoteViews.setTextViewText(R.id.widget_text_holiday, output.substring(2));
appWidgetManager.updateAppWidget(i, remoteViews);
}
}

还有我的一个小部件布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_relative_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rounded_widget_dark"
android:orientation="vertical" >

<TextView
android:id="@+id/widget_text_today"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="@string/today_is"
android:textColor="#FFF"
android:textSize="20sp" />

<TextView
android:id="@+id/widget_text_holiday"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/widget_text_today"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textColor="#FFF" />

</RelativeLayout>

如何让它发挥作用?我不知道什么可能是不正确的...

最佳答案

这是我的工作代码。这会起作用。

 for (int i = 0; i < N; i++) {
int awID = ids[i];

RemoteViews views = new RemoteViews(getPackageName(),
R.layout.widget_layout);

Intent intent = new Intent(this, your intented class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

//views.setTextViewText(R.id.widgetcount, visitstext);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, intent, 0);

views.setOnClickPendingIntent(R.id.widgetimage, pendingIntent);
manager.updateAppWidget(awID, views);
}

关于Android - 小部件点击监听器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35391040/

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