gpt4 book ai didi

java - 小部件点击无响应

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

我正在尝试为小部件中的所有 View 设置一个点击监听器,该监听器应该在点击时打开一个 Activity ,但目前看来它不起作用。谁能告诉我我做错了什么?

这是我的代码:

  public class AthanWidget extends AppWidgetProvider {

private SharedPreferences prefs;
private boolean PAID = false;

public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;

prefs = context.getSharedPreferences(context.getPackageName(),
Context.MODE_PRIVATE);
SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy");
String currentDateandTime = sdf.format(new Date());
PAID = prefs.getBoolean(Constants.ITEM_PURCHASED, false);

// TODO CHECK IF IMAGE IS FROM SD OR A RESOURCE IS

// Perform this loop procedure for each App Widget that belongs to this
// provider

for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];

// Create an Intent to launch ExampleActivity
Intent intent;
if (!PAID) {
intent = new Intent(context, RemoveAds.class);
} else {
intent = new Intent(context, Main.class);
}
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 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.widget_layout);

views.setOnClickPendingIntent(appWidgetId, pendingIntent);

views.setTextViewText(R.id.widget_date_title, currentDateandTime);
views.setTextViewText(R.id.widget_arabic_date_title, arabianDate());

displayPrayerTimes(views);
if (prefs.getInt(Constants.BG_ID_PREFS, 1) < 0) {
Bitmap myBitmap = BitmapFactory.decodeFile(prefs.getString(
Constants.BG_SD_PATH, ""));
views.setImageViewBitmap(R.id.widget_bg, myBitmap);
} else {
views.setImageViewResource(R.id.widget_bg,
prefs.getInt(Constants.BG_ID_PREFS, R.drawable.bg24));
}

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

private void displayPrayerTimes(RemoteViews views) {
if (PAID) {
views.setTextViewText(
R.id.widget_fajr,
prefs.getString(Constants.FARJ_ID + "w", "DISABLED").split(
",")[0]);
views.setTextViewText(R.id.widget_dhurh,
prefs.getString(Constants.DHUHR_ID + "w", "DISABLED")
.split(",")[0]);
views.setTextViewText(
R.id.widget_isha,
prefs.getString(Constants.ISHA_ID + "w", "DISABLED").split(
",")[0]);
views.setTextViewText(
R.id.widget_asr,
prefs.getString(Constants.ASR_ID + "w", "DISABLED").split(
",")[0]);
}

views.setTextViewText(
R.id.widget_shorrok,
prefs.getString(Constants.SHORROK_ID + "w", "DISABLED").split(
",")[0]);
views.setTextViewText(
R.id.widget_maghrib,
prefs.getString(Constants.MAGHRIB_ID + "w", "DISABLED").split(
",")[0]);
}

private String arabianDate() {
Date date = new Date(); // هنا التاريخ الصليبي
Calendar cl = Calendar.getInstance();
cl.setTime(date);
final String[] iMonthNames = { "Muharram", "Safar", "Rabi'ul Awwal",
"Rabi'ul Akhir", "Jumadal Ula", "Jumadal Akhira", "Rajab",
"Sha'ban", "Ramadan", "Shawwal", "Dhul Qa'ada", "Dhul Hijja" };

Chronology iso = ISOChronology.getInstanceUTC();
Chronology hijri = IslamicChronology.getInstanceUTC();

LocalDate todayIso = new LocalDate(cl.get(Calendar.YEAR),
cl.get(Calendar.MONTH) + 1, cl.get(Calendar.DAY_OF_MONTH), iso);
LocalDate todayHijri = new LocalDate(todayIso.toDateTimeAtStartOfDay(),
hijri);

return todayHijri.getDayOfMonth() + " "
+ iMonthNames[todayHijri.getMonthOfYear()] + " "
+ todayHijri.getYear();
}

}

和我的小部件布局:

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

<ImageView
android:id="@+id/widget_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha=".85"
android:contentDescription="@string/app_name"
android:scaleType="centerCrop" />

<TextView
android:id="@+id/widget_toptext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingTop="10dp"
android:text="@string/widget_title"
android:textColor="@android:color/white"
android:textSize="15sp" />

<TextView
android:id="@+id/widget_date_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/widget_toptext"
android:layout_centerHorizontal="true"
android:textColor="@android:color/white"
android:textSize="15sp" />

<TextView
android:id="@+id/widget_arabic_date_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/widget_date_title"
android:layout_centerHorizontal="true"
android:textColor="@android:color/white"
android:textSize="15sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#80000000"
android:baselineAligned="false"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp" >

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/w_pray_sun_up"
android:drawableRight="@drawable/w_pray_sun_up"
android:gravity="bottom"
android:paddingRight="2dp"
android:text="@string/ar_fajr"
android:textColor="@android:color/white"
android:textSize="12sp" />

<TextView
android:id="@+id/widget_fajr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/voir"
android:textColor="@android:color/white"
android:textSize="12sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fajr"
android:textColor="@android:color/white"
android:textSize="12sp" />
</LinearLayout>

<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="@android:color/white" />
<!-- delete this -->

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/w_pray_sun_up"
android:drawableRight="@drawable/w_pray_sun_up"
android:gravity="bottom"
android:paddingRight="2dp"
android:text="@string/ar_shorok"
android:textColor="@android:color/white"
android:textSize="12sp" />

<TextView
android:id="@+id/widget_shorrok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/voir"
android:textColor="@android:color/white"
android:textSize="12sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/shorrok"
android:textColor="@android:color/white"
android:textSize="12sp" />
</LinearLayout>

<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="@android:color/white" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/w_pray_dhurh"
android:drawableRight="@drawable/w_pray_dhurh"
android:gravity="bottom"
android:paddingRight="2dp"
android:text="@string/ar_dhuhr"
android:textColor="@android:color/white"
android:textSize="12sp" />

<TextView
android:id="@+id/widget_dhurh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/voir"
android:textColor="@android:color/white"
android:textSize="12sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dhuhr"
android:textColor="@android:color/white"
android:textSize="12sp" />
</LinearLayout>

<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="@android:color/white" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/w_pray_asr"
android:drawableRight="@drawable/w_pray_asr"
android:gravity="bottom"
android:paddingRight="2dp"
android:text="@string/ar_asr"
android:textColor="@android:color/white"
android:textSize="12sp" />

<TextView
android:id="@+id/widget_asr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/voir"
android:textColor="@android:color/white"
android:textSize="12sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/asr"
android:textColor="@android:color/white"
android:textSize="12sp" />
</LinearLayout>

<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="@android:color/white" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/w_pray_maghreb"
android:drawableRight="@drawable/w_pray_maghreb"
android:gravity="bottom"
android:paddingRight="2dp"
android:text="@string/ar_magrib"
android:textColor="@android:color/white"
android:textSize="12sp" />

<TextView
android:id="@+id/widget_maghrib"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/voir"
android:textColor="@android:color/white"
android:textSize="12sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/maghrib"
android:textColor="@android:color/white"
android:textSize="12sp" />
</LinearLayout>

<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="@android:color/white" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/w_pray_moon"
android:drawableRight="@drawable/w_pray_moon"
android:gravity="bottom"
android:paddingRight="2dp"
android:text="@string/ar_isha"
android:textColor="@android:color/white"
android:textSize="12sp" />

<TextView
android:id="@+id/widget_isha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/voir"
android:textColor="@android:color/white"
android:textSize="12sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/isha_a"
android:textColor="@android:color/white"
android:textSize="12sp" />
</LinearLayout>

<!-- end of deletion -->
</LinearLayout>

</RelativeLayout>

最佳答案

在这里,您正在为 appwidgetId 设置点击等待 Intent ,但它应该是 View ID。

RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
views.setOnClickPendingIntent(appWidgetId, pendingIntent);

喜欢:

如果 Button 的 id 是 R.id.button1。如果您想为该按钮设置点击监听器,它位于小部件布局中。那应该是这样的:

 RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
views.setOnClickPendingIntent(R.id.button1, pendingIntent);

关于java - 小部件点击无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28215092/

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