Android selector press to modify the implementation code of background and text color
1. Press selector to modify the background and text color
【1】 Click change font color - Android: state_ Pressed
【2】 The selector status selector (bg_btn_two (storing res - drawable)) is under the resource file, otherwise it will not work
【3】 Selector status selector (bg_btn_one (storing res - color)) under the resource file, otherwise it will not work
【3】android:state_ Pressed = "true" must be placed in the first line, otherwise it will not take effect
2. Implement demo
【1】 Effect achieved:
【2】 Implementation code
Create selector file
Contents of the selector file under the color file
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/colorAccent" android:state_pressed="true"/> //按下时显示 <item android:drawable="@color/colorPrimary" /> //默认显示 </selector>
Contents of selector file under drawable file
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/colorAccent" android:state_pressed="true"/> <item android:drawable="@color/colorPrimary" /> </selector>
Calling XML file: Android: background = "@ drawable / bg_btn_two" modify background
android:textColor="@color/bg_btn_one" 修改文字 <Button android:background="@drawable/bg_btn_two" android:textColor="@color/bg_btn_one" android:layout_width="match_parent" android:layout_height="45dp" android:text="点击改变背景颜色" android:textSize="20sp" android:gravity="center" />
summary
The above is the implementation code of Android selector that Xiaobian introduced to you. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support to our website! If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!