[英]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.