如何更改小部件按钮的图像?

6
我正在尝试找到一种方法来更改按钮的图像,以显示应用程序的当前状态。
到目前为止,我已经做了以下工作...
HelloWidgetProvider.java
public class HelloWidgetProvider extends AppWidgetProvider {



@Override
public void onDeleted(Context context, int[] appWidgetIds) {
// TODO Auto-generated method stub
//super.onDeleted(context, appWidgetIds);
Toast.makeText(context, "onDeleted()", Toast.LENGTH_LONG).show();
}

@Override
public void onDisabled(Context context) {
// TODO Auto-generated method stub
//super.onDisabled(context);
Toast.makeText(context, "onDisabled()", Toast.LENGTH_LONG).show();
}



@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
//super.onEnabled(context);
Toast.makeText(context, "onEnabled()", Toast.LENGTH_LONG).show();
}

public static String MY_WIDGET_UPDATE = "MY_OWN_WIDGET_UPDATE";

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
 int[] appWidgetIds) {
// TODO Auto-generated method stub


CharSequence strWidgetText = "App Status";

RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.hellowidget_layout);

updateViews.setTextViewText(R.id.widgettext, strWidgetText);
appWidgetManager.updateAppWidget(appWidgetIds, updateViews);

super.onUpdate(context, appWidgetManager, appWidgetIds);
Toast.makeText(context, "onUpdate()", Toast.LENGTH_LONG).show();

 }




@Override
 public void onReceive(Context context, Intent intent) {
  // TODO Auto-generated method stub
  super.onReceive(context, intent);

//  if(MY_WIDGET_UPDATE.equals(intent.getAction())){
   Toast.makeText(context, "onReceiver()", Toast.LENGTH_LONG).show();
  //}
 }

}
<?xml version="1.0" encoding="utf-8"?>

hellowidget_layout.xml

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="wrap_content"
 android:layout_height="fill_parent"
 >
<Button
android:id="@+id/widgettext"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:textColor="#000000"
 android:background="@drawable/icon"
 android:text="Test Button"
 />
</LinearLayout>

hellowidgetproviderinfo.xml

 <?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="146dp"
    android:minHeight="72dp"
    android:updatePeriodMillis="1000"
    android:initialLayout="@layout/hellowidget_layout"
>
</appwidget-provider>

请帮助我... 谢谢
1个回答

8

我已经实现了一个小部件,其中我展示了ImageButton作为ToggleButton来改变图像和文本。

这是ToggleImageWidget类:

public class ToggleImageWidget extends AppWidgetProvider {
    private static boolean status = false;
    private RemoteViews remoteViews;
    private ComponentName watchWidget;
    @Override
    public void onUpdate( Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds )
    {
        remoteViews = new RemoteViews( context.getPackageName(), R.layout.main );
        watchWidget = new ComponentName( context, ToggleImageWidget.class );
        //remoteViews.setTextViewText( R.id.tbutton, "Time = " + format.format( new Date()));
        Intent intentClick = new Intent(context,ToggleImageWidget.class);
        intentClick.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, ""+appWidgetIds[0]);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetIds[0],intentClick, 0);
        remoteViews.setOnClickPendingIntent(R.id.img_btn, pendingIntent);
        appWidgetManager.updateAppWidget( watchWidget, remoteViews );
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if (intent.getAction()==null) {
            Bundle extras = intent.getExtras();
            if(extras!=null) {
                //int widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);

                remoteViews = new RemoteViews( context.getPackageName(), R.layout.main );
                if(status){
                    remoteViews.setImageViewResource(R.id.img_btn, R.drawable.radio_btn_off_img);
                    remoteViews.setTextViewText(R.id.widget_textview, "Off");
                    status = false;
                }else{
                    remoteViews.setImageViewResource(R.id.img_btn, R.drawable.radio_btn_img);
                    remoteViews.setTextViewText(R.id.widget_textview, "On");
                    status = true;
                }

                watchWidget = new ComponentName( context, ToggleImageWidget.class );

                (AppWidgetManager.getInstance(context)).updateAppWidget( watchWidget, remoteViews );
                //Toast.makeText(context, "Clicked "+status, 2000).show();
            }
        }
        else {
            super.onReceive(context, intent);
        }
    }
}

这是我的main.xml文件代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_height="50dp"
    android:background="#55ffffff"
    android:layout_width="100dp"
    android:orientation="horizontal" android:gravity="center_vertical|center_horizontal">
     <ImageButton android:layout_width="32dp" android:layout_height="32dp"
     android:id="@+id/img_btn" android:background="@drawable/radio_btn_off_img" />
     <TextView
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:paddingLeft="5dp"
        android:textColor="@android:color/black"
        android:text="Off"
        android:textSize="8pt" 
        android:id="@+id/widget_textview"/>
</LinearLayout>

这是我的AndroidManifest.xml文件代码

<application android:icon="@drawable/icon" android:label="@string/app_name">
        <receiver android:name=".ToggleImageWidget" android:label="ToggleImageWidget">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/img_widget_provider" />
        </receiver>

    </application>

这是我的img_widget_provider.xml文件代码,它存储在res/xml目录中,如果不存在,请创建xml目录到res目录中。

<?xml version="1.0" encoding="utf-8" ?>
<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="146dp"
    android:initialLayout="@layout/main"
    android:updatePeriodMillis="1000"
    android:minHeight="144dp"/>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接