RadioGroup和CheckBox使用示例

简介: MainActivity如下:package cc.cv;import cn.com.bravesoft.testchoose.R;import android.
MainActivity如下:
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>



相关文章
|
2月前
|
JavaScript 前端开发
checkbox中checked属性总结
checkbox中checked属性总结
59 0
|
7月前
|
容器
Qt6学习笔记七(ToolButton、RadioButton、GroupBox、CheckBox、ListWidget、TreeWidget、TableWidget)
Qt6学习笔记七(ToolButton、RadioButton、GroupBox、CheckBox、ListWidget、TreeWidget、TableWidget)
258 0
|
C#
艾伟_转载:C# WinForm开发系列 - CheckBox/Button/Label/ProgressBar
包含自定义颜色显示的CheckBox,水晶效果按钮,透明圆角Label,Vista效果的ProgressBar等控件(文章及相关代码搜集自网络,仅供参考学习,版权属于原作者! ). 1.自定义颜色显示的CheckBox coloredcheckcontrols.
1475 0
自定义Checkbox和Radiobox
在线演示 本地下载
899 0
|
JavaScript 前端开发 Windows
|
索引
Android--listView中的button控件获取item的值
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/55253877 在listv...
1220 0
|
前端开发 Android开发
CheckBox
写一个带动画化效果的CheckBox,先看效果,我觉得这个尺寸是最好看的,画图板画出来的,有点丑啊。。 003.png 000.
942 0