实现界面:
复选框经典用法、自定义复选框按钮、设置复选框监听
常用属性:
android:text 设置选项文本
android:button 自定义复选框按钮
android:textSize 设置文本字体大小
android:checked="true" 设置默认选中
android:textColor 设置文本颜色
android:textSize 设置文本大小
代码实现:
activity_check_box.xml:布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_check_box"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.exercise.CheckBoxActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="你会哪些编程语言?"
android:textSize="16sp"
android:textColor="@color/colorBlack"
android:id="@+id/tv_1"
android:layout_marginBottom="10dp"/>
<!--设置复选框的字体、选中、内容-->
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_1"
android:text="Java"
android:checked="true"
android:textColor="@color/colorOrange"
android:textSize="14sp"
android:id="@+id/cb_1"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cb_1"
android:text="C++"
android:checked="true"
android:textColor="@color/colorOrange"
android:textSize="14sp"
android:id="@+id/cb_2"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cb_2"
android:text="Python"
android:textColor="@color/colorOrange"
android:textSize="14sp"
android:id="@+id/cb_3"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cb_3"
android:text="PHP"
android:textColor="@color/colorOrange"
android:textSize="14sp"
android:id="@+id/cb_4"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cb_4"
android:text="C"
android:textColor="@color/colorOrange"
android:textSize="14sp"
android:id="@+id/cb_5"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="你有哪些爱好?"
android:id="@+id/tv_2"
android:textSize="16sp"
android:textColor="@color/colorBlack"
android:layout_below="@+id/cb_5"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<!--设置自定义选项框-->
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_2"
android:text="编程"
android:textColor="@color/colorOrange"
android:button="@drawable/selector_orange_checkbox"
android:paddingLeft="5dp"
android:textSize="14sp"
android:id="@+id/cb_6"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cb_6"
android:text="马云演讲"
android:paddingLeft="5dp"
android:button="@drawable/selector_orange_checkbox"
android:textColor="@color/colorOrange"
android:textSize="14sp"
android:id="@+id/cb_7"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/cb_7"
android:paddingLeft="5dp"
android:text="励志电影"
android:button="@drawable/selector_orange_checkbox"
android:textColor="@color/colorOrange"
android:textSize="14sp"
android:id="@+id/cb_8"/>
</RelativeLayout>
CheckBoxActivity:java文件
package com.example.administrator.exercise;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
public class CheckBoxActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{
private CheckBox checkBox,checkBox1,checkBox2,checkBox3,checkBox4,checkBox5,checkBox6,
checkBox7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box);
checkBox = (CheckBox) findViewById(R.id.cb_1);
checkBox.setOnCheckedChangeListener(this);
checkBox1 = (CheckBox) findViewById(R.id.cb_2);
checkBox1.setOnCheckedChangeListener(this);
checkBox2 = (CheckBox) findViewById(R.id.cb_3);
checkBox2.setOnCheckedChangeListener(this);
checkBox3 = (CheckBox) findViewById(R.id.cb_4);
checkBox3.setOnCheckedChangeListener(this);
checkBox4 = (CheckBox) findViewById(R.id.cb_5);
checkBox4.setOnCheckedChangeListener(this);
checkBox5 = (CheckBox) findViewById(R.id.cb_6);
checkBox5.setOnCheckedChangeListener(this);
checkBox6 = (CheckBox) findViewById(R.id.cb_7);
checkBox6.setOnCheckedChangeListener(this);
checkBox7 = (CheckBox) findViewById(R.id.cb_8);
checkBox7.setOnCheckedChangeListener(this);
}
//监听器的参数
//第一个参数:被点击的按钮
//第二个参数:是否选中
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
Toast.makeText(CheckBoxActivity.this,b?compoundButton.getText():"未选中",Toast.LENGTH_SHORT).show();
}
}
selector_orange_checkbox.xml:自定义背景文件
选中时选取背景1,未选中时选取背景2
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/icon_checked_true"/>
<item android:state_checked="false" android:drawable="@drawable/icon_checked_false"/>
</selector>