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

时间:2011-03-03 15:21:29

标签: java android android-widget

我试图找到一种方法来更改按钮的图像,以便显示应用程序的当前状态。

这是我到目前为止所做的......

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 个答案:

答案 0 :(得分:8)

我实现了小部件,因为我将ImageButton显示为togglebutton以更改图像和文本

这是ToogleImageWidget类

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目录中,如果它不存在,你已经在res目录中创建了xml目录。

<?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"/>