开发者

How to change the text color when the button is being pressed?

开发者 https://www.devze.com 2023-01-06 03:24 出处:网络
I am implementing something like the user interface from Microsoft Zune HD player. So, I would like t开发者_运维百科o change the text color of my button when the button is being pressed as well as whe

I am implementing something like the user interface from Microsoft Zune HD player. So, I would like t开发者_运维百科o change the text color of my button when the button is being pressed as well as when it has been clicked.


Case solved. I just added an XML file into my color folder. Add in a selector XML and change the attribute "textColor" of my button to that selector XML.

Further reference - Android selector & text color


In res/drawable, create a file called for instance mybutton_background.xml and put something like this inside:

<?xml version="1.0" encoding="utf-8"?>
<selector android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false"
android:drawable="@drawable/button_background_focus" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/button_background_pressed" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/button_background_pressed" />
<item drawable="@drawable/button_background_normal">
</selector>

Then set this drawable as the background of your button with android:background="@drawable/mybutton_background".

0

精彩评论

暂无评论...
验证码 换一张
取 消