1. 实验内容简介
多项选择(CheckBox)组件也被称为复选框,该组件通常用于某选项的打开或关闭。CheckBox表明一个特定的状态是勾选(on,值为1)还是不勾选(off,值为0),在应用程序中为用户提供“真”或“假”选择。复选框状态彼此独立,因此可同时选择任意多个。
通过鼠标单击复选框,可触发复选框状态的改变。复选框会从当前状态变到另一种状态。
使用步骤:
步骤1:声明CheckBox变量
步骤2:通过FindViewById关联或绑定
步骤3:监听用户输入动作
2. UI界面布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Title"/>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:text="@string/mz_balin" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/checkBox1"
android:text="@string/mz_buyi" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/checkBox2"
android:text="@string/mz_gaoshan"/>
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/checkBox3"
android:text="@string/mz_shezu"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/checkBox4"
android:layout_marginLeft="24dp"
android:text="@string/Submit"/>
</RelativeLayout>
在res->values->string.xml加入:
<?xmlversion="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">CheckBoxDemo</string>
<string name="hello_world">Hello world!</string>
<string name="Title">下列属于中国的56个民族的是哪些?</string>
<string name="mz_gaoshan">高山族</string>
<string name="mz_shezu">畲族</string>
<string name="mz_buyi">布依族</string>
<string name="mz_balin">巴林族</string>
<string name="Submit">提交</string>
</resources>
3. 代码编写和调试
public class MainActivity extends Activity {
CheckBox checkbox1;
CheckBox checkbox2;
CheckBox checkbox3;
CheckBox checkbox4;
Button button;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkbox1 = (CheckBox)findViewById(R.id.checkBox1);
checkbox2 = (CheckBox)findViewById(R.id.checkBox2);
checkbox3 = (CheckBox)findViewById(R.id.checkBox3);
checkbox4 = (CheckBox) findViewById(R.id.checkBox4);
checkbox1.setOnCheckedChangeListener(new CheckBoxListener());
checkbox2.setOnCheckedChangeListener(new CheckBoxListener());
checkbox3.setOnCheckedChangeListener(new CheckBoxListener());
checkbox4.setOnCheckedChangeListener(new CheckBoxListener());
button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(checkbox2.isChecked()&&checkbox3.isChecked() &&checkbox4.isChecked() )
{
Toast.makeText(MainActivity.this,"答案正确", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this,"答案错误", Toast.LENGTH_SHORT).show();
}
}
});
}
class CheckBoxListener implementsOnCheckedChangeListener {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked)
{
Toast.makeText(MainActivity.this,"被选择", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this,"取消选择", Toast.LENGTH_SHORT).show();
}
}
}
}