如何在状态更改时更改Android ToogleButton的文本颜色?

gue*_*gue 9 android text colors togglebutton

我的toggle-Button为每个状态(红色和白色)提供不同颜色的背景.现在我需要在激活时更改togglebutton文本(红色/白色)的颜色.使用xml我无法让它工作,也许任何人都知道我做错了什么?

布局xml中的我的按钮:

<ToggleButton 
android:paddingRight="20dip" 
android:id="@+id/pseudo_tab_right" 
android:layout_weight=".50" 
android:layout_width="wrap_content" 
android:textStyle="bold" 
android:paddingLeft="10dip" 
android:textSize="12sp" 
android:layout_height="wrap_content" 
android:textColor="@drawable/pseudo_tab_text_color"
android:textOff="@string/pseudo_tab_right_text"
android:textOn="@string/pseudo_tab_right_text"
android:background="@drawable/tab_button_right" 
/>  
Run Code Online (Sandbox Code Playgroud)

按钮状态的xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/sort_button_red_right_43" /> 
<item android:drawable="@drawable/sort_button_white_right_43" />
</selector>
Run Code Online (Sandbox Code Playgroud)

和xml的颜色:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:state_pressed="true" android:color="#4f5459" /> 

<!-- focused -->
<item android:state_focused="true" android:color="#4f5459" />

<!-- default -->
<item android:color="#ffffff" /> 

<!-- trying these out, but none works -->
<item android:state_checked="true" android:color="#ff0000" />
<item android:state_enabled="true" android:color="#ff00dd" />
<item android:state_selected="true" android:color="#ff00dd" />
<item android:state_active="true" android:color="#ff00dd" />

</selector>
Run Code Online (Sandbox Code Playgroud)

gue*_*gue 15

找到它:现在我正在使用android:state_checked="true"android:state_checked="false".

颜色的XML:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="#ffffff" />
    <item android:state_checked="false" android:color="#000000" />
</selector>
Run Code Online (Sandbox Code Playgroud)


Mik*_*e D -1

您已使用下面的示例,将颜色属性移动到项目标签中。

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

<item android:state_pressed="true" >
    <shape>
        <corners
            android:topLeftRadius="3dp"
            android:bottomLeftRadius="0.1dp"
            android:topRightRadius="0.1dp"
            android:bottomRightRadius="3dp" />
        <gradient
            android:startColor="#d0785e"
            android:endColor="#000000"
            android:angle="270" />
    </shape>
</item>

<item android:state_focused="true" >
    <shape>
        <corners
            android:topLeftRadius="3dp"
            android:bottomLeftRadius="0.1dp"
            android:topRightRadius="0.1dp"
            android:bottomRightRadius="3dp" />
        <gradient
            android:endColor="#ffffff"
            android:startColor="#b9b9b9"
            android:angle="270" />
    </shape>
</item>

<item android:state_enabled="false">
    <shape>
        <corners
            android:topLeftRadius="3dp"
            android:bottomLeftRadius="0.1dp"
            android:topRightRadius="0.1dp"
            android:bottomRightRadius="3dp" />
        <gradient
            android:startColor="#f0aa9f"
            android:endColor="#e21f00"
            android:angle="270" />
    </shape>
</item>

<item>        
    <shape>
        <corners
            android:topLeftRadius="3dp"
            android:bottomLeftRadius="0.1dp"
            android:topRightRadius="0.1dp"
            android:bottomRightRadius="3dp" />
        <gradient
            android:startColor="#fe9c69"
            android:endColor="#fc5700"
            android:angle="270" />
    </shape>
</item>


</selector>
Run Code Online (Sandbox Code Playgroud)