I have a ToggleButton that uses a selector to choose between 2 images (checked and unchecked).
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@drawable/img_piece1" /> <!-- pressed -->
<item android:draw开发者_StackOverflow中文版able="@drawable/img_piece2" /> <!-- default/unchecked -->
</selector>
Is there a way to retrieve the current drawable resource (checked/unchecked) of the toggle button in code? I tried using the ToggleButton.getBackground(), however this only seems to return the default Drawable always.
I figured out how to do this and so am answering my own question. :)
Apparently the current drawable can be retrieved with ToggleButton.getBackground().getCurrent()
Hope it helps.
i was in same situation few days ago.. but made my way out by giving its TEXT ON ="." and TextOFF as "" ie blank... that dot wont be seem on that image (probably) coz mine did not. works well then check the text on toggle button.
Though I haven't ever played with it, you may want to take a look at getDrawableState()
. It may have what you're looking for.
精彩评论