Android button is clicked text color change effect

Source: Internet
Author: User

Sometimes do the application need to click on the button when the text color also changed, released and restored, now found two solutions: the first use of pictures, if there are more places, then the amount of the picture is considerable; second, that is what this article describes. Talk less, first put the picture, then the code.

Normal effect:

Press the effect:

Create the Color.xml file in the values directory and add the following custom color (note that it is not a color tag) code:

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <drawable name= "Red" > #f00 </drawable >    <drawable name= "green" > #0f0 </drawable>    <drawable name= "Gray" > #ccc </drawable ></resources>

Then create a new drawable directory under RES and create a new btn_bg.xml and Btn_color.xml file with the following code:

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 the test:

<?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 text will change effect"Android:textcolor= "@drawable/btn_color"Android:background= "@drawable/btn_bg"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "button is disabled"android:enabled= "false"Android:textcolor= "@drawable/btn_color"Android:background= "@drawable/btn_bg"/> </LinearLayout>

OK, you are done!

Original: http://blog.csdn.net/maylian7700/article/details/6978131

Android button is clicked text color change effect

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.