开发者

ImageButton in Android homescreen widget

开发者 https://www.devze.com 2023-01-07 03:12 出处:网络
I have a homescreen widget with an imagebutton. I have the button working with a pending i开发者_如何学Pythonntent, but I can\'t seem to figure out how to change the button image when it is pressed.

I have a homescreen widget with an imagebutton. I have the button working with a pending i开发者_如何学Pythonntent, but I can't seem to figure out how to change the button image when it is pressed.

I tried using a selector and it works in my widget test activity, but not in the remoteview.

How could I implement this functionality in the home screen widget?


You can set a different Image within your remote view.

remoteView.setInt(R.id.buttonimg, "setImageResource", R.drawable.button_on);


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)?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号