CheckBox是多选方框,具有选中和未选中两种状态,他不像单选按钮在一个组中只能选择一个,而checkBox我们一次可以选中多个多选按钮!
CheckBox相应的属性及方法:
默认选中:android:checked="true";可以在xml文件中多选标签Checkbox添加如下属性,此标签默认为选中;
isChecked()判断按钮是否处于被选中状态
setChecked(Boolean flag) 传递一个布尔参数来设置按钮的状态
CheckBox.getText();//获取多选框的值
Android复选框被选择时处理方法
setOnCheckedChangeListener()方法
Checkbox的直接父类是CompoundButton,间接父类是Button;
定义布局文件checkbox.xml文件,内容如下
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" >
<TextView
android:id="@+id/checkbox_text_id"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/Textview_content_web"
>
</TextView>
<CheckBox
android:id="@+id/checkbox_android_id"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Android"
/>
<CheckBox
android:id="@+id/checkbox_ios_id"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="IOS"
/>
<CheckBox
android:id="@+id/checkbox_winphone_id"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Windows Phone"
/>
</LinearLayout>
</ScrollView>
定义java文件
package com.dream.app.start.first.checkbox;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.dream.app.start.R;
import com.dream.app.start.R.id;
import com.dream.app.start.R.layout;
import com.dream.app.start.R.string;
import com.dream.app.start.utils.PublicClass;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class CheckBoxDemo extends Activity {
TextView textview_button=null;
private CheckBox checkbox_Anroid,checkbox_Ios,checkboxWinphone;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_first_checkbox);
//通过id获取id对应的组件
checkbox_Anroid = (CheckBox)findViewById(R.id.checkbox_android_id);
checkbox_Ios = (CheckBox)findViewById(R.id.checkbox_ios_id);
checkboxWinphone = (CheckBox)findViewById(R.id.checkbox_winphone_id);
//组件点击点击事件
checkbox_Anroid.setOnClickListener(checklistener);
checkbox_Ios.setOnClickListener(checklistener);
checkboxWinphone.setOnClickListener(checklistener);
}//点击事件
private OnClickListener checklistener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView txt = (TextView)v;
switch (txt.getId()) {
case R.id.checkbox_android_id:
if(checkbox_Anroid.isChecked())
{
showToast("你选中的是:"+txt.getText().toString());
}else
{
showToast("你取消的是:"+txt.getText().toString());
}
break;
case R.id.checkbox_ios_id:
if(checkbox_Ios.isChecked())
{
showToast("你选中的是:"+txt.getText().toString());
}else
{
showToast("你取消的是:"+txt.getText().toString());
}
break;
case R.id.checkbox_winphone_id:
if(checkboxWinphone.isChecked())
{
showToast("你选中的是:"+txt.getText().toString());
}else
{
showToast("你取消的是:"+txt.getText().toString());
}
break;
default:
break;
}
}
};
/**
* toast
* @param str
*/
private void showToast(String str) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
}
}
效果图:
[追加]:
1.继承自CompoundButton类的方法:
setChecked(boolean checked);//Changes the checked state of this button.
设置checkbox的check状态。
2.继承自View类的方法:
setSelected(boolean selected);//Changes the selection state of this view.
3.checked与selection两种状态的区别:
看看这段代码就明白了,无论是否checked,只要点击了这个checkbox,就被认为是selection。
import android.widget.CompoundButton.OnCheckedChangeListener;
...
CheckBox checkbox = (CheckBox)findViewById(R.id.checkbox);
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
String string = checkbox.isChecked() ? "Checked" : "Selected";
checkbox.setText(string);
}
});
并不能改变checkbox的是否被check的状态。