Android Custom Image Button Does Not Stay Selected
I am currently using an ImageButton, and I want to have the effect like radio button once you select it stays selected, until someone picks another image button. I have setup custom selector like below:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/buttonimagesel" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/buttonimagesel" /> <!-- focused -->
<item android:drawable="@drawable/buttonimage" /> <!-- default -->
</selector>
But this just shows th开发者_如何学JAVAe selected image for as long as the key is pressed down. The effect i want is for it to stay selected like a radio button until the request is processed after which the whole activity including the button is redrawn. So I want one click to put the button in a selected state and unclick does not change this. Also I do not want the other buttons to be selectable after this happens, and I don't certainly don't want them to change images or anything like that.
Thanks
If you need to use an ImageButton
, you can add an android:state_selected="true"
item and use setSelected()
in your onClick()
logic. You would have to take care of deselecting all the other buttons when selecting a new one. This question might be useful: Android ImageButton with a selected state?
However you could also just use RadioButton
s and customize their look (with android:background
and android:button
- these and all CompoundButton
s have a checked state that work in a toggling way).
精彩评论