我们常常会写button 点击变色(获焦点),失焦恢复颜色这样:
很简单,在drawable写一个xml,selector中,判断 textcolor 就可以了,
selector还可以写其他有趣的事情,比如描边,圆角,填充,设置内边距等等;
但是在button中是无效的!如果用代码写的话,又太费劲....
<Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="无价值" android:textColor="@drawable/auto_textonclick" android:background="@drawable/auto_bgonclick" android:layout_marginLeft="10dp" android:gravity="center"/>
正常的Button 是没用的,所以,我们用RadioButton !
<!-- 评分按钮 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="10dp"> <RadioGroup android:layout_width="match_parent" android:layout_height="35dp" android:orientation="horizontal"> <RadioButton android:id="@+id/noJz" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="无价值" android:textColor="@drawable/auto_textonclick" android:background="@drawable/auto_bgonclick" android:button="@null" android:layout_marginLeft="10dp" android:gravity="center"/> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="还可以" android:textColor="@drawable/auto_textonclick" android:background="@drawable/auto_bgonclick" android:button="@null" android:gravity="center" android:layout_marginLeft="10dp"/> <RadioButton android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="有价值" android:textColor="@drawable/auto_textonclick" android:background="@drawable/auto_bgonclick" android:button="@null" android:gravity="center" android:layout_marginLeft="10dp"/> </RadioGroup> </LinearLayout>
auto_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 得到焦点:白色 -->
<item
android:color="@android:color/white"
android:state_checked="true" />
<!-- 默认:红色 -->
<item
android:color="@android:color/holo_red_light"/>
</selector>
auto_bgonclick
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/all_red"
android:state_checked="true"/>
<item
android:drawable="@drawable/bai_red"/>
</selector>
selector还可以写其他有趣的事情,比如描边,圆角,填充,设置内边距等等:
iconbg.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 描边 --> <stroke android:width="0.5dp" android:color="@color/Chuise3"/> <!-- 填充 --> <solid android:color="@android:color/white"/> <!-- 圆角 --> <corners android:radius="8dp"/> <!-- 内容与边框间距 --> </shape>
背景引用:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_margin="5dp" android:padding="10dp" android:background="@drawable/iconbg"> .......内容 </LinearLayout>
是不是很简单啊?