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