问题描述
我米试图找到一种方法来改变按钮我为了显示应用程序的当前状态的图像。
i"m trying to find a way to change the image of the button i order to show the current state of the application.
这是我迄今所做的......
this is what i have done so far...
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>
请帮我....谢谢
推荐答案
我已经实现了小部件,我有显示的ImageButton的切换按钮来改变图片和文字
I have implement the widget in that I have display ImageButton as togglebutton to change the image and text
下面是ToogleImageWidget类
Here is the ToogleImageWidget class
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文件code
here is my main.xml file code
<?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文件code
here is my AndroidManifest.xml file code
<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>
这里是存储在RES我img_widget_provider.xml文件code / xml目录必须创建XML目录到资源目录,如果它不存在。
and here is my img_widget_provider.xml file code which is stored in res/xml directory you have create the xml dir into the res dir if it doesn't exist.
<?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"/>
这篇关于我们怎样才能改变的小窗口按钮的形象呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!