[英]change background color of image button in custom notification when clicked
我有一個包含四個圖像按鈕的自定義通知布局。
用戶單擊它們時,我需要更改圖像按鈕的背景顏色。
我需要用
ImageButton imgButton = (ImageButton) findViewById(R.id.recentAppButt);
imgButton.setBackgroundColor(Color.BLUE);
但這會引起nullpointerexception錯誤,因為編譯器找不到自定義通知布局中的最近使用的圖像按鈕,因為這些圖像按鈕在自定義通知布局中。
這是來自mainActivity.class的一些代碼,我在其中創建或刪除通知
private void createNotificationIcons() {
String ns = Context.NOTIFICATION_SERVICE;
notificationManager = (NotificationManager) getSystemService(ns);
notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.home)
.setContentTitle("My notification")
.setContentText("All Buttons!")
.setOngoing(true) /** notification will appear as ongoing notification*/
.build();
/** set a custom layout to the notification in notification drawer */
RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.notification);
notification.contentView = notificationView;
Intent recentAppIntent = new Intent(Context.NOTIFICATION_SERVICE);
recentAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle recentAppBundle = new Bundle();
recentAppBundle.putInt("userAnswer", 1);
recentAppIntent.putExtras(recentAppBundle);
PendingIntent pendingrecentAppIntent = PendingIntent.getBroadcast(getBaseContext(), 1, recentAppIntent, 0);
notificationView.setOnClickPendingIntent(R.id.recentAppButt, pendingrecentAppIntent);
notificationManager.notify(1, notification);
}
public class SwitchButtonListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle answerBundle = intent.getExtras();
int userAnswer = answerBundle.getInt("userAnswer");
if(userAnswer == 1) {
ImageButton imgButton = (ImageButton) findViewById(R.id.recentAppButt);
imgButton.setBackgroundColor(Color.BLUE);
Toast.makeText(context, "Recent Apps", Toast.LENGTH_SHORT).show();
Log.d("Recent", "recent app butt clicked");
openRecentApps();
}
}
}
public class BackgroundService extends Service {
MainActivity mainActivity = new MainActivity();
MainActivity.SwitchButtonListener switchButtonListener = mainActivity.new SwitchButtonListener();
/** Called when the service is being created. */
@Override
public void onCreate() {
super.onCreate();
}
/** The service is starting, due to a call to startService() */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
registerReceiver(switchButtonListener, new IntentFilter(Context.NOTIFICATION_SERVICE));
return START_STICKY;
}
/** Called when The service is no longer used and is being destroyed */
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(switchButtonListener);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
這是notification.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@android:style/TextAppearance.StatusBar.EventContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent" >
<!-- create home button in notification drawer -->
<ImageButton
android:id="@+id/homeButt"
android:layout_width="79dp"
android:layout_height="79dp"
android:layout_marginLeft="1dp"
android:background="@android:color/transparent"
android:contentDescription="@string/discription"
android:src="@drawable/home" />
<!-- create recent apps button in notification drawer -->
<ImageButton
android:id="@+id/recentAppButt"
android:layout_width="79dp"
android:layout_height="79dp"
android:background="@android:color/transparent"
android:contentDescription="@string/discription"
android:src="@drawable/recent" />
<!-- create lock screen button in notification drawer -->
<ImageButton
android:id="@+id/lockScreenButt"
android:layout_width="79dp"
android:layout_height="79dp"
android:background="@android:color/transparent"
android:contentDescription="@string/discription"
android:src="@drawable/lock" />
<!-- create allButtons button in notification drawer -->
<ImageButton
android:id="@+id/allButtonsButt"
android:layout_width="79dp"
android:layout_height="79dp"
android:background="#FF990000"
android:contentDescription="@string/discription"
android:src="@drawable/all_buttons" />
提前致謝。
檢查布局文件中的ImageView是否具有將height和width的參數設置為wrap_parent 。
如果其中任何一個(layout_height或layout_width)設置為wrap_content ,並且您在ImageView上沒有設置實際圖像,則將其高度設置為0。換句話說,您應該將wrap_content更改為另一個值,例如以100dp為例,看看顏色是否改變。
findViewById是Activity類的一種方法,不適用於RemoteViews和通知。 您收到空值異常,因為R.id.recentAppButt在您的活動視圖中不存在。
您可以通過在drawables文件夾中創建兩個具有不同背景顏色的圖標來實現所需的功能。 然后,可以使用RemoteViews類的setImageViewResource方法在兩個圖標之間切換,如下所示
notificationView.setImageViewResource(R.id.recentAppButt, R.drawable.icon_with_blue_background)
然后,您需要重建通知並通過NotificationManager重新通知 ,以便更新背景色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.