MainActivity如下:
main.xml如下:
package cc.cv; import cn.com.bravesoft.testchoose.R; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView; import android.widget.RadioGroup.OnCheckedChangeListener; /** * Demo描述: * RadioGroup和CheckBox使用示例 * */ public class MainActivity extends Activity { private RadioGroup mRadioGroup; private CheckBox mEatCheckBox; private CheckBox mSleepCheckBox; private CheckBox mPlayCheckBox; private Button mConfirmButton; private TextView mResultTextView; private StringBuffer hobby; private String gender=""; private String selectedResult=""; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mResultTextView = (TextView) findViewById(R.id.resultTextView); mRadioGroup = (RadioGroup) findViewById(R.id.genderRadioGroup); mRadioGroup.setOnCheckedChangeListener(new RadioButtonOnCheckedChangeListenerImpl()); mEatCheckBox=(CheckBox) findViewById(R.id.eatCheckBox); mSleepCheckBox=(CheckBox) findViewById(R.id.sleepCheckBox); mPlayCheckBox=(CheckBox) findViewById(R.id.playBox); mConfirmButton = (Button) findViewById(R.id.confirmButton); mConfirmButton.setOnClickListener(new ButtonOnClickListenerImpl()); } private class ButtonOnClickListenerImpl implements OnClickListener{ @Override public void onClick(View view) { hobby=new StringBuffer(); selectedResult=new String(); //获取被选中的单选按钮及其内容 for (int i = 0; i < mRadioGroup.getChildCount(); i++) { RadioButton radioButton=(RadioButton) mRadioGroup.getChildAt(i); if (radioButton.isChecked()) { gender=radioButton.getText().toString(); break; } } //判断每个CheckBox是否被选中 if (mEatCheckBox.isChecked()) { hobby.append(" "); hobby.append(mEatCheckBox.getText().toString()); } if (mSleepCheckBox.isChecked()) { hobby.append(" "); hobby.append(mSleepCheckBox.getText().toString()); } if (mPlayCheckBox.isChecked()) { hobby.append(" "); hobby.append(mPlayCheckBox.getText().toString()); } selectedResult=gender+" "+hobby.toString(); mResultTextView.setText(selectedResult); } } //监听单选的变化 private class RadioButtonOnCheckedChangeListenerImpl implements OnCheckedChangeListener{ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton rb=(RadioButton)findViewById(group.getCheckedRadioButtonId()); String currentSelected=rb.getText().toString(); System.out.println("现在选中的性别是:"+currentSelected); }} }
main.xml如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center_horizontal" > <TextView android:id="@+id/resultTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#00FF00" android:textSize="20dip" > </TextView> <RadioGroup android:id="@+id/genderRadioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/male" > </RadioButton> <RadioButton android:id="@+id/female" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/female" > </RadioButton> </RadioGroup> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <CheckBox android:id="@+id/eatCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/eat" > </CheckBox> <CheckBox android:id="@+id/sleepCheckBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sleep" > </CheckBox> <CheckBox android:id="@+id/playBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/play" > </CheckBox> </LinearLayout> <Button android:id="@+id/confirmButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dip" android:text="@string/ok" > </Button> </LinearLayout>
strings.xml如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">TestChoose</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="male">男</string> <string name="female">女</string> <string name="eat">吃饭</string> <string name="sleep">睡觉</string> <string name="play">玩耍</string> <string name="ok">选择完毕</string> </resources>