通过自定义checkbox和点击响应事件说说<selector>
1.首先在drawable文件夹中添加checkbox_style.xml
<pre class="html" name="code"><span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"></span>
<span style="font-size:14px;">
<item android:drawable="@drawable/checkbox_pressed" android:state_checked="true"/>
<item android:drawable="@drawable/checkbox_normal" android:state_checked="false"/>
<item android:drawable="@drawable/checkbox_normal"/></span>
<span style="font-size:14px;">
</selector></span>
2.在values文件夹下的styles.xml文件中添加CustomCheckboxTheme样式。
<pre class="html" name="code"><span style="font-size:14px;"><style name="</span><span style="font-size:14px;color:#ff0000;">CustomCheckboxTheme</span><span style="font-size:14px;">" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/</span><span style="font-size:14px;color:#3333ff;">checkbox_style</span><span style="font-size:14px;"></item>
</style> </span>
<span style="font-size:18px;">3.在布局文件中使用</span><span style="font-size:18px;color:#ff0000;">CustomCheckboxTheme</span><span style="font-size:18px;">样式。</span>
<span id="_xhe_cursor"><span style="font-size:18px;"></span>
</span>
<span style="font-size:14px;"><CheckBox
android:id="@+id/select_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/</span><span style="font-size:14px;color:#ff0000;">CustomCheckboxTheme</span><span style="font-size:14px;">" /> </span><span style="font-size:18px;">
</span>
以上内容转自 http://blog.youkuaiyun.com/zuolongsnail/article/details/7106586 ------------------------------------
通过查阅总结,首先android的selector是在drawable/xxx.xml中配置的,常用属性有:
android:state_selected 选中
android:state_focused 获得焦点
android:state_pressed 点击
android:state_enabled 设置是否响应事件,指所有事件
根据这些状态同样可以设置button的selector效果。也可以设置selector改变button中的文字状态。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#FFF" />
<item android:state_focused="true" android:color="#FFF" />
<item android:state_pressed="true" android:color="#FFF" />
<item android:color="#000" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<!-- 定义当button 处于pressed 状态时的形态。-->
<shape>
<gradient android:startColor="#8600ff" />
<stroke android:width="2dp" android:color="#000000" />
<corners android:radius="5dp" />
<padding android:left="10dp" android:top="10dp"
android:bottom="10dp" android:right="10dp" />
</shape>
</item>
<item android:state_focused="true">
<!-- 定义当button获得focus时的形态-->
<shape>
<gradient android:startColor="#eac100" />
<stroke android:width="2dp" android:color="#333333" color="#ffffff" />
<corners android:radius="8dp" />
<padding android:left="10dp" android:top="10dp"
android:bottom="10dp" android:right="10dp" />
</shape>
</item>
</selector>
最后,需要在包含button的xml文件里添加两项。假如是main.xml 文件,我们需要在<Button />里加两项。
android:focusable="true"
android:backgroud="@drawable/button_color"
这样当你使用Button的时候就可以甩掉系统自带的那黄颜色的背景了,实现个性化的背景。