Sometimes, when the app needs to click a button, the text color also changes. After the app is released, it is restored. Currently, two solutions are found: first, use images. If there are many images, the number of images is considerable. Second, this is what we will talk about in this article. To put it bluntly, paste the image first and then add the code.
Normal effect:
Effect:
First, create the color. xml file in the values directory, and add the following custom color code (note that the color label is not used) to it:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Drawable name = "red"> # f00 </drawable>
<Drawable name = "green"> #0f0 </drawable>
<Drawable name = "gray"> # ccc </drawable>
</Resources>
Create the drawable directory under res, and create the btn_bg.xml and btn_color.xml files. The Code is as follows:
Btn_bg.xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: state_window_focused = "false" android: state_enabled = "true"
Android: drawable = "@ drawable/btn_test_normal"/>
<Item android: state_enabled = "false" android: drawable = "@ drawable/btn_test_normal"/>
<Item android: state_pressed = "true" android: drawable = "@ drawable/btn_test_press"/>
<Item android: state_focused = "true" android: drawable = "@ drawable/btn_test_normal"/>
</Selector>
Btn_color.xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: android = "http://schemas.android.com/apk/res/android">
<Item android: state_focused = "false" android: state_enabled = "true" android: state_pressed = "false"
Android: color = "@ drawable/red"/>
<Item android: state_enabled = "false" android: color = "@ drawable/gray"/>
<Item android: state_pressed = "true" android: color = "@ drawable/green"/>
<Item android: state_focused = "true" android: color = "@ drawable/red"/>
</Selector>
Finally, the layout file for testing:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "@ android: color/white"
>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "press the text to change the effect"
Android: textColor = "@ drawable/btn_color"
Android: background = "@ drawable/btn_bg"
/>
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "the button is disabled"
Android: enabled = "false"
Android: textColor = "@ drawable/btn_color"
Android: background = "@ drawable/btn_bg"
/>
</LinearLayout>
OK, success
From jacp's column