Android:切换按钮OnPress状态

时间:2015-05-27 16:23:31

标签: android selector drawable state

我的ToggleButton有一个selector backgroundDrawable。 ON和OFF状态工作完全正常,即它们根据我的需要改变颜色。但是,我试图在按下Togglebutton时对其进行着色,即触摸并不释放。

selector:     

<item android:drawable="@drawable/category_unselected_background"
      android:state_checked="false"/>

<item android:drawable="@drawable/category_highlighted_background"
      android:state_pressed="true"/>

<item android:drawable="@drawable/category_unselected_background"/>

我做错了什么?

4 个答案:

答案 0 :(得分:2)

这是我用于任何我正在使用的按钮选择器的代码,总是像魅力一样,适合自己:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">


<!-- Button Focused-->
<item android:drawable="@drawable/plus_icon_pressed" android:state_focused="true" android:state_pressed="false" />
<!-- Button Focused Pressed-->
<item android:drawable="@drawable/plus_icon_pressed" android:state_focused="true" android:state_pressed="true" />
<!-- Button Pressed-->
<item android:drawable="@drawable/plus_icon_pressed" android:state_focused="false" android:state_pressed="true" />
<!-- Button Default Image-->
<item android:drawable="@drawable/plus_icon" />


</selector>

答案 1 :(得分:1)

以下情况适用于所有州。由于我使用ToggleButton,我也需要checked状态:

<!-- Button Focused-->
<item android:drawable="@drawable/category_highlighted_background" android:state_focused="true" android:state_pressed="false" />
<!-- Button Focused Pressed-->
<item android:drawable="@drawable/category_highlighted_background" android:state_focused="true" android:state_pressed="true" />
<!-- Button Pressed-->
<item android:drawable="@drawable/category_highlighted_background" android:state_focused="false" android:state_pressed="true" />
<!-- Button Pressed-->
<item android:drawable="@drawable/category_selected_background" android:state_checked="true" />
<!-- Button Default Image-->
<item android:drawable="@drawable/category_unselected_background" />

答案 2 :(得分:0)

这是一个例子:),here

   <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <item android:state_checked="true" android:state_pressed="true" /> //currently pressed turning the toggle on
            <item android:state_pressed="true" /> //currently pressed turning the toggle off
            <item android:state_checked="true" /> //not pressed default checked state
            <item /> //default non-pressed non-checked
        </selector>

答案 3 :(得分:0)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Button Touch-->
    <item android:drawable="@drawable/icon_background_pressed" android:state_pressed="true" />
    <!-- Button ON-->
    <item android:drawable="@drawable/icon_background_selected" android:state_checked="true" />
    <!-- Button Default Image-->
    <item android:drawable="@drawable/icon_background_normal" />
</selector>