How to change the text color of an Android ToogleButton on state change?

只谈情不闲聊 提交于 2019-12-21 09:19:22

问题


my toggle-Button has different colored backgrounds for each state(red and white). Now I need to change the color of the togglebutton-text(red/white) when activated. With xml I just cannot get it working, perhaps anybody has an idea what I'm doing wrong?

My button in the Layout 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" 
/>  

xml for button-states:

<?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>

And xml for color:

<?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>

回答1:


Found it: now I'm using android:state_checked="true" and android:state_checked="false".

color-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>



回答2:


You have use something the example below, move the colour attributes to be contained in the item tags.

<?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>


来源:https://stackoverflow.com/questions/7096599/how-to-change-the-text-color-of-an-android-tooglebutton-on-state-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!