android,自定义 state_pressed 不起作用

android, custom state_pressed doesn't work

我下面的自定义选择器没有按预期工作,我根本看不到 pressed 状态。任何提示?谢谢

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

    >
    <!-- this state button has special design:
        + enabled state has white text on green background
        + disabled state has grey text on greyer background
    -->


    <item android:state_enabled="true"
        android:drawable="@drawable/cb_shape_button_primary"
        android:color="@color/white"

        />
    <item android:state_pressed="true"
        android:drawable="@drawable/cb_shape_button_pressed"
        android:color="@color/white"

        />

    <item android:state_focused="true"
        android:drawable="@drawable/cb_shape_button_pressed"
        android:color="@color/white"

        />
    <item android:state_enabled="false"
        android:drawable="@drawable/cb_shape_button_flat"
        android:color="@color/grey_subtext"
        />

</selector>

尝试像这样将 state_pressed 移到顶部:

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

>
<!-- this state button has special design:
    + enabled state has white text on green background
    + disabled state has grey text on greyer background
-->

<item android:state_pressed="true"
    android:drawable="@drawable/cb_shape_button_pressed"
    android:color="@color/white"

    />

<item android:state_enabled="true"
    android:drawable="@drawable/cb_shape_button_primary"
    android:color="@color/white"

    />

<item android:state_focused="true"
    android:drawable="@drawable/cb_shape_button_pressed"
    android:color="@color/white"

    />
<item android:state_enabled="false"
    android:drawable="@drawable/cb_shape_button_flat"
    android:color="@color/grey_subtext"
    />

编辑:
正如 @forcewill 提到的,请注意将按下状态移动到顶部的原因与 Android 将使用匹配的第一个状态这一事实有关,在你的情况是,小部件是同时启用和按下的,所以它首先匹配启用状态。

选择器 XML 标签总是选择 第一个 匹配项。这意味着如果启用视图,它将始终显示 @drawable/cb_shape_button_primary。选择器的一般经验法则是您的 "default" 项目应该是列表中的最后一个。

最后尝试移动默认状态,看看是否能解决您的问题。

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

    >
    <!-- this state button has special design:
        + enabled state has white text on green background
        + disabled state has grey text on greyer background
    -->

    <item android:state_pressed="true"
        android:drawable="@drawable/cb_shape_button_pressed"
        android:color="@color/white"

        />

    <item android:state_focused="true"
        android:drawable="@drawable/cb_shape_button_pressed"
        android:color="@color/white"

        />
    <item android:state_enabled="false"
        android:drawable="@drawable/cb_shape_button_flat"
        android:color="@color/grey_subtext"
        />

   <item android:state_enabled="true"
        android:drawable="@drawable/cb_shape_button_primary"
        android:color="@color/white"

        />

</selector>

试试这个,

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/select_item" android:state_checked="true" android:state_focused="true" />
<item android:drawable="@drawable/unselect_item" android:state_checked="false" android:state_focused="true" />
<item android:drawable="@drawable/unselect_item" android:state_checked="false" />
<item android:drawable="@drawable/select_item" android:state_checked="true" />

对于简单 Select 和取消选择使用这个

<selector><item android:state_checked="false" android:drawable="@drawable/radiobtnunselected"/><item android:state_checked="true" android:drawable="@drawable/radiobtnselected"/></selector>

并将您的图像设置为 android:drawable

正确:Selected

False:未选中