CheckBox:复选框
1.有两种状态:
选中状态(true),未选中状态(false)
2.属性:
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="男"
CheckBox的默认android:checked属性为false。
checkBox的OnCheckedChangeListener事件检查勾是否勾选。
样例程序中有3个CheckBox和1个TextView,TextView事实演示了有多少CheckBox被勾选了以及被勾选的CheckBox的名称。
<LinearLayout 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" android:orientation="vertical" > <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="篮球" /> <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="足球" /> <CheckBox android:id="@+id/checkBox3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="乒乓球" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0项选择" /> </LinearLayout>
package com.example.checkbox; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.TextView; public class MainActivity extends ActionBarActivity { private CheckBox basketballCheckBox; private CheckBox footballCheckBox; private CheckBox pingpongCheckBox; private boolean[] checkedArray = new boolean[] {false, false, false}; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); basketballCheckBox = (CheckBox) findViewById(R.id.checkBox1); footballCheckBox = (CheckBox) findViewById(R.id.checkBox2); pingpongCheckBox = (CheckBox) findViewById(R.id.checkBox3); textView = (TextView) findViewById(R.id.textView1); basketballCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { checkedArray[0] = isChecked; textViewResetValue(); } }); footballCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { checkedArray[1] = isChecked; textViewResetValue(); } }); pingpongCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { checkedArray[2] = isChecked; textViewResetValue(); } }); } private void textViewResetValue() { String values = ""; int sumChecked = 0; for (boolean val : checkedArray) if (val == true) sumChecked += 1; if (sumChecked == 0) textView.setText("0 项选择"); else { if (checkedArray[0] == true) values += ",篮球"; if (checkedArray[1] == true) values += ",足球"; if (checkedArray[2] == true) values += ",乒乓球"; values = sumChecked + "项选择:" + values.substring(1); textView.setText(values); } } }
效果:
注:Android有一个自己的log记录函数:Log.i()。