简体   繁体   English

以编程方式更改Android小部件的ImageButton的src图像

[英]Change src image of ImageButton of Android widget programatically

I am developing an Android app with a homescreen widget, which is an imageButton, and i want the image of the button to change after its been pressed. 我正在开发带有主屏幕小部件(即imageButton)的Android应用,并且我希望在按下按钮后更改按钮的图像。 I have tried selector in an xml file, to change the image of the button, but it changes it only when it is pressed, but it goes back when its released. 我已经尝试在xml文件中使用选择器来更改按钮的图像,但是仅在按下按钮时它会更改它,但是在释放按钮时它会返回。

I want it to change image after it was pressed and when the code inside the WidgetProvider is executed, it changes the image back to the default one. 我希望它在按下图像后更改图像,并且在执行WidgetProvider内的代码时,它将图像更改回默认图像。

I tried to do it programatically(in onRecieve() method), I guess it should be something with RemoteViews but whatever i try, it doesnt work. 我试图以编程方式做到这一点(在onRecieve()方法中),我想它应该与RemoteViews兼容,但是无论我如何尝试,都无法正常工作。

Could someone help my with this? 有人可以帮我吗? I saw many people asking about it, but most of the posts were either pretty old or the solutions were not working for me. 我看到很多人都在问这个问题,但是大多数帖子要么很陈旧,要么解决方案对我不起作用。

Thank you :) 谢谢 :)

Normally you'd set the image on the surface of the ImageButton using the setImageResource method ( ImageButton inherits it from ImageView ), but that method isn't available for RemoteViews . 通常,您可以使用setImageResource方法在ImageButton的表面上设置图像( ImageButton继承自ImageView ),但是该方法不适用于RemoteViews

RemoteViews offers a corresponding setImageViewResource method that does what you want (as well as setImageViewBitmap and setImageViewUri depending on how you'd like to provide the image data). RemoteViews提供了一个相应的setImageViewResource方法,该方法可以执行您想要的操作(以及setImageViewBitmapsetImageViewUri,具体取决于您要如何提供图像数据)。

The code to set the image (say, within your BroadcastReceiver's onReceive method) would look something like this: 设置图像的代码(例如,在BroadcastReceiver的onReceive方法中)如下所示:

@Override
public void onReceive(Context context, Intent intent) {
    int appWidgetId = intent.getIntExtra("appWidgetId", -1);    
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
    views.setImageViewResource(R.id.imageButton1, R.drawable.awesome_image);
    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
    appWidgetManager.updateAppWidget(appWidgetId, views);
}

Note that the code above assumes you've added the id of the widget to be manipulated as an extra to the broadcast intent (wrapped in a PendingIntent that is sent when the user clicks the ImageButton) using the key "appWidgetId". 请注意,上面的代码假定您已使用键“ appWidgetId”将广播控件的ID作为附加控件添加到广播意图(包装在用户单击ImageButton时发送的PendingIntent中)。 You'll most likely want to do that in the onUpdate method of your AppWidgetProvider, like so: 您很可能希望在AppWidgetProvider的onUpdate方法中执行此操作,如下所示:

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (int i = 0; i < appWidgetIds.length; i++) {
        int appWidgetId = appWidgetIds[i];
        Intent intent = new Intent ("YourActionHere");
        intent.putExtra("appWidgetId", appWidgetId);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, intent, 0);

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
        views.setImageViewResource(R.id.imageButton1, R.drawable.initial_image);

        views.setOnClickPendingIntent(R.id.imageButton1, pendingIntent);
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

A simple but not elegant way is: 一种简单但不优雅的方法是:

 boolean isButtonSelected = false;

    ..
 button.setOnClickListener(buttonListener);

    ..

 OnClickListener buttonListener = new OnClickListener() {
  @Override
  public void onClick(View arg0) {

      if(isButtonSelected ){
          isButtonSelected = false;
          button.setBackgroundResource(R.drawable.buttonOff);  // or setImageResource(..);
      } else{
          isButtonSelected = true;
          button.setBackgroundResource(R.drawable.buttonOn);
      }
   }
 };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM