5

我有一个带有图像按钮的主屏幕小部件。我的按钮正在处理一个未决的意图,但我似乎无法弄清楚如何在按下按钮时更改按钮图像。

我尝试使用选择器,它适用于我的小部件测试活动,但不适用于远程视图。

如何在主屏幕小部件中实现此功能?

4

2 回答 2

3

您可以在远程视图中设置不同的图像。

remoteView.setInt(R.id.buttonimg, "setImageResource", R.drawable.button_on);
于 2010-09-02T09:53:43.053 回答
0

I was able to get it working by moving the selector inside it's own XML file and putting in res/drawable and then referencing that xml file as my resource for my imagebutton in the app-widget's layout xml.

buttonimg.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
 android:state_pressed="false"
 android:drawable="@drawable/button_off" />
<item
 android:state_pressed="true"
 android:drawable="@drawable/button_on" />
<item  
 android:drawable="@drawable/button_off" />
</selector>

widget_layout.xml

<ImageButton
android:id="@+id/buttonimg"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_x="0px"
android:layout_y="2px"
android:clickable="true"
android:src="@drawable/button"
>

I now have a different problem, however. The selector only allows me to change the image onPress. On release, the image changes back >:-(

How do I get the image state change on press and stay in that state until next press (like the ToggleButton)?

于 2010-07-15T17:20:56.310 回答