Android中CheckBox与CompoundButton源码解析
经历过了前⾯⼀系列的讲解,下⾯我们直接来看看系统⾥⾯的CheckBox与CompoundButton类的源码⽂件。你肯定会发现很多熟悉的地⽅。
结合下⾯源码,我们对它们进⾏解析解析,它⾥⾯使⽤的就是⾃定义drawable state。
我们⾸先直接看CheckBox的源码
public class CheckBox extends CompoundButton {
public CheckBox(Context context) {
this(context, null);
}
public CheckBox(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.checkboxStyle);
}
public CheckBox(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
event.setClassName(Name());
}
@Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
info.setClassName(Name());
}
}
我们可以看到,它⾥⾯并没有做什么,因为它的操作都是继承⾃CompoundButton.
我们先来看看下⾯⼀个普通的布局⽂件:
<RelativeLayout xmlns:android="schemas.android/apk/res/android"
xmlns:tools="schemas.android/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox"/>
</RelativeLayout>
在上⾯的这个相对布局中,就写了⼀个CheckBox,我们什么都不做,直接运⾏代码,就会看到下⾯的运⾏界⾯:
当我们单击这个CheckBox,我们发现他会被选中,那么它内部到底是怎么实现的呢?
其实使⽤过drawable state的⼈应该对这个并不陌⽣,我们经常这样做:
1、在res/drawable⽂件下创建l,⽰例代码如下:
2、编写布局⽂件,为布局⽂件中的ImageButton设置selector,⽰例代码如下:
当我们单击这个图⽚按钮的时候,图⽚按钮的图⽚就会发⽣变化,其实CheckBox也是这样做的,只是这些系统为我们做了。
下⾯我们来看看系统实现源码: 上⾯在布局⽂件中直接写了⼀个CheckBox,布局⽂件被解析后就会实例化这个CheckBox对象,就会执⾏CheckBox的构造函数:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android ="schemas.android/apk/res/android">
<item
android:state_pressed ="false"
android:drawable ="@drawable/title_button_back">
</item >
<item
android:state_pressed ="true"
android:drawable ="@drawable/title_button_back_h">
</item >
<item
android:state_window_focused ="false"
android:drawable ="@drawable/title_button_back">
</item >
</selector >
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android ="schemas.android/apk/res/android"
android:layout_height ="wrap_content"
android:layout_width ="fill_parent">
<ImageButton
android:id ="@+id/title_IB"
android:layout_height ="wrap_content"
android:layout_width ="wrap_content"
android:background ="#00000000"
android:layout_marginRight ="4dp"
android:layout_centerVertical ="true"
android:src ="@drawable/selector">
</ImageButton >
</RelativeLayout >
public CheckBox(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.checkboxStyle);
}
public CheckBox(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
从上⾯我们可以知道两点:
1、CheckBox的默认样式是com.android.internal.R.attr.checkboxStyle
2、最终执⾏的时候CompoundButton的构造函数
在frameworks/base/core/res/res/l⽂件中,已经初始化了checkBoxStyle样式:<item name="checkboxStyle">@android:style/Widget.CompoundButton.CheckBox</item>
在frameworks/base/core/res/res/l⽂件,我们来看看它的样式是什么:
<style name="Widget.CompoundButton.CheckBox">
<item name="android:button">?android:attr/listChoiceIndicatorMultiple</item>
</style>
在frameworks/base/core/res/res/l⽂件:
<item name="listChoiceIndicatorMultiple">@android:drawable/btn_check</item>
可以看到,CheckBox的默认样式就是给它的button属性赋值了⼀个btn_check,我们来看看btn_check⽂件⾥⾯的具体内容。在frameworks/base/core/res/res/drawable/l
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="schemas.android/apk/res/android">
<!-- Enabled states -->
<item android:state_checked="true"android:state_window_focused="false"
android:state_enabled="true"
android:drawable="@drawable/btn_check_on" />
<item android:state_checked="false"android:state_window_focused="false"
android:state_enabled="true"
android:drawable="@drawable/btn_check_off" />
<item android:state_checked="true"android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_on_pressed" />
<item android:state_checked="false"android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_off_pressed" />
<item android:state_checked="true"android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_on_selected" />
<item android:state_checked="false"android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_off_selected" />
<item android:state_checked="false"
android:state_enabled="true"
android:drawable="@drawable/btn_check_off" />
<item android:state_checked="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_on" />
<!-- Disabled states -->
<item android:state_checked="true"android:state_window_focused="false"
android:drawable="@drawable/btn_check_on_disable" />
<item android:state_checked="false"android:state_window_focused="false"
android:drawable="@drawable/btn_check_off_disable" />
<item android:state_checked="true"android:state_focused="true"
android:drawable="@drawable/btn_check_on_disable_focused" />
<item android:state_checked="false"android:state_focused="true"
nodeselectorandroid:drawable="@drawable/btn_check_off_disable_focused" />
<item android:state_checked="false"android:drawable="@drawable/btn_check_off_disable" /> <item android:state_checked="true"android:drawable="@drawable/btn_check_on_disable" />
</selector>
我们看到,state_checked为true的时候,drawable=”@drawable/btn_check_on”,state_checked为false的时
候,drawable=”@drawable/btn_check_off”.
@drawable/btn_check_on图⽚和@drawable/btn_check_off图⽚,在frameworks/base/core/res/res/drawable我们可以到:
我们可以看到,其实就是根据不同的状态,为button属性赋值了不同的图⽚资源,这就是我们看到的效果。
state_checked这个状态的定义,在下⾯进⾏了定义:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论