我有一个ToggleButton,它使用一个选择器来选择2个图像(选中和未选中)。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="@drawable/img_piece1" /> <!-- pressed -->
<item android:drawable="@drawable/img_piece2" /> <!-- default/unchecked -->
</selector>
有没有办法检索代码中切换按钮的当前可绘制资源(已选中/未选中)?我尝试使用ToggleButton.getBackground(),但这似乎只返回默认的Drawable。
答案 0 :(得分:3)
我想出了如何做到这一点,所以我回答了我自己的问题。 :)
显然可以使用ToggleButton.getBackground()检索当前drawable。 getCurrent()
希望它有所帮助。
答案 1 :(得分:1)
前几天我的情况相同...... 但是通过给它的TEXT ON =“。”来解决问题。和TextOFF为“”即空白... 那点不会出现在那个图像上(可能)因为我没有。 效果很好 然后检查切换按钮上的文字。
答案 2 :(得分:0)
虽然我从未玩过它,但您可能需要查看getDrawableState()
。它可能有你想要的东西。