CheckBox:複選框
1.有兩種狀態:
選中狀態(true),未選中狀態(false)
2.屬性:
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="男"java
CheckBox的默認android:checked屬性爲false。
checkBox的OnCheckedChangeListener事件檢查勾是否勾選。
樣例程序中有3個CheckBox和1個TextView,TextView事實演示了有多少CheckBox被勾選了以及被勾選的CheckBox的名稱。android
<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); } } }
效果:app
注:Android有一個本身的log記錄函數:Log.i()。ide