Android开发笔记(七)——CheckBox

RadioButton可以实现在一组控件中单选的功能,CheckBox可以实现复选的功能。
首先依旧是新建一个用于演示CheckBox的activity CheckBoxActivity ,此时 AndroidMainfest.xml 中会自动添加如下声明:

<activity android:name=".CheckBoxActivity"></activity>

之后在 activity_main.xml 中添加一个Button用来进行RadioButton控件的展示:

<Button
    android:
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="CheckBox"
    android:textAllCaps="false"
    />

接下来在 MainActivity.java 中声明这个控件:

private Button mBtnCheckBox;

之后要在 MainActivity.java 中的 onCreate 函数中使用 findViewById 找到该button:

mBtnCheckBox=findViewById(R.id.btn_radiobutton);

setListener() 中设置监听事件:

mBtnCheckBox.setOnClickListener(onClick);

之后在 onClick 中添加:

case R.id.btn_checkbox:
    //跳转到CheckBox演示界面
    intent=new Intent(MainActivity.this, CheckBoxActivity.class);
    break;

具体可以看上次的笔记。
之后打开布局文件 activity_check_box.xml 进行布局:

常用属性

和之前的内容差不多,下面是一个布局的例子:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    >

   <TextView
       android:
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="你会哪些移动开发?"
       android:textSize="20sp"
       android:textColor="#000000"
       android:layout_marginBottom="20dp"
       />

   <CheckBox
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android"
        android:textSize="20sp"
        android:layout_below="@id/tv_title"
        />

   <CheckBox
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="IOS"
        android:textSize="20sp"
        android:layout_below="@id/cb_1"
        />

   <CheckBox
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="H5"
        android:textSize="20sp"
        android:layout_below="@id/cb_2"
        />

   <CheckBox
        android:
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="其他"
        android:textSize="20sp"
        android:layout_below="@id/cb_3"
        />
</RelativeLayout>

效果:

Android开发笔记(七)——CheckBox

自定义样式

通常实例开发中都是采用自定义的样式:
同样先进行布局:

   <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="vertical"
       android:layout_below="@id/cb_4"
       android:layout_marginTop="20dp"
       >
       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="你的兴趣爱好有哪些?"
           android:textSize="20sp"
           android:textColor="#000000"
           />

       <CheckBox
           android:
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="唱歌"
           android:textSize="20sp"
           android:layout_marginTop="20dp"
           />

       <CheckBox
           android:
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="绘画"
           android:textSize="20sp"
           />

       <CheckBox
           android:
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="其他"
           android:textSize="20sp"
           />
   </LinearLayout>

把准备好的样式图片放置到 drawable-xxhdpi 中,再新建一个 drawable resource file
Android开发笔记(七)——CheckBox

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:andro>
    <item android:state_checked="false"
        android:drawable="@drawable/icon_checkbox_false"/>
    <item android:state_checked="true"
        android:drawable="@drawable/icon_checkbox_ture"/>
</selector>

背景选择器准备好之后,返回布局文件加入属性:

android:button="@drawable/bg_checkbox"
android:paddingLeft="10dp"

其中 paddingLeft 是添加内边距。
效果:

Android开发笔记(七)——CheckBox

监听事件

CheckBoxActivity.java 中首先声明:

private CheckBox mCb5,mCb6,mCb7;

之后在 onCreate 函数中找到这几个当选中状态进行变化时想要有监听事件的控件:

mCb5=findViewById(R.id.cb_5);
mCb6=findViewById(R.id.cb_6);
mCb7=findViewById(R.id.cb_7);

之后再设置监听事件:

mCb5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Toast.makeText(CheckBoxActivity.this,isChecked?"选中":"未选中",Toast.LENGTH_SHORT).show();
    }
});

其他两个按钮设置方法一样。
效果:

Android开发笔记(七)——CheckBox