XML中:
<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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity" >
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="@+id/man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="man" />
<RadioButton
android:id="@+id/woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="woman" />
<CheckBox
android:id="@+id/basketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篮球" />
<CheckBox
android:id="@+id/pingpang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="乒乓球" />
<CheckBox
android:id="@+id/football"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球" />
</RadioGroup>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="saveAll"
android:text="save" />
<Button
android:id="@+id/reset"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="reset"
android:text="reset" />
</LinearLayout>
</LinearLayout>
MainActivity中:
package com.example.checkboxtext;
import android.os.Bundle;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
private int [] ids = {R.id.basketball,R.id.pingpang,R.id.football};
private CheckBox[] checkBoxs;
private RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg = (RadioGroup) findViewById(R.id.radiogroup);
checkBoxs = new CheckBox[ids.length];
for(int i = 0;i<ids.length;i++){
checkBoxs[i] = (CheckBox) this.findViewById(ids[i]);
/*
*
* checkBoxs[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox checkBox = (CheckBox) v;
Toast.makeText(MainActivity.this,checkBox.getText().toString()+""+checkBox.isChecked(),0 ).show();
//获取选取的选项卡内容和是否被选中
}
});
* */
/*
* 状态改变监听
* */
checkBoxs[i].setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, buttonView.getText().toString()+","+isChecked, 0).show();
}
});
}
}
public void reset(View view){
for(CheckBox cb:checkBoxs){
cb.setChecked(false);
}
}
public void saveAll(View view){
int id = rg.getCheckedRadioButtonId();//获取选择的单选按钮id
StringBuffer stringBuffer = new StringBuffer();
RadioButton radioButton = (RadioButton) this.findViewById(id);//根据id点获取选中单选按钮
stringBuffer.append(radioButton.getText().toString()+",");//获取按钮上 的字符串进行字符串拼接
/*
* 获取选中的复选框内容
* */
for(CheckBox cb :checkBoxs){
if(cb.isChecked()){
stringBuffer.append(cb.getText().toString()+",");
}
}
Toast.makeText(this, stringBuffer.toString(), 0).show();
}
}
常用监听事件:
获取选中的内容:
public void saveAll(View view){
int id = rg.getCheckedRadioButtonId();//获取选择的单选按钮id
StringBuffer stringBuffer = new StringBuffer();
RadioButton radioButton = (RadioButton) this.findViewById(id);//根据id点获取选中单选按钮
stringBuffer.append(radioButton.getText().toString()+",");//获取按钮上 的字符串进行字符串拼接
/*
* 获取选中的复选框内容
* */
for(CheckBox cb :checkBoxs){
if(cb.isChecked()){
stringBuffer.append(cb.getText().toString()+",");
}
}
Toast.makeText(this, stringBuffer.toString(), 0).show();
}
复选框重置:
public void reset(View view){
for(CheckBox cb:checkBoxs){
cb.setChecked(false);
}
}
给复选框每一个选项添加监听事件之setOnClickListener
checkBoxs[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CheckBox checkBox = (CheckBox) v;
Toast.makeText(MainActivity.this,checkBox.getText().toString()+""+checkBox.isChecked(),0 ).show();
//获取选取的选项卡内容和是否被选中
}
});
给复选框每一个选项添加监听事件之setOnCheckedChangeListener
checkBoxs[i].setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, buttonView.getText().toString()+","+isChecked, 0).show();
}
});
注意:setOnClickListener与setOnCheckedChangeListener的区别:
前者是点击选择时才执行点击选中,点击取消两种情况
后者是状态只有一改变就会触发执行,状态改变有多种情况:比如点击选中,点击取消,点击重置都可触发