The android button is clicked to change the text color.

Source: Internet
Author: User

 

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.