RadioBox单选框应用

时间:2024-03-23 07:43:02

一、RadioBox单选框的使用

 

public RadioGroup mRadioGroup1;
	public RadioButton mRadio1, mRadio2;
	public Button button1;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.radio);
		button1 = (Button) findViewById(R.id.button1);
		mRadioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
		mRadio1 = (RadioButton) findViewById(R.id.radio1);
		mRadio2 = (RadioButton) findViewById(R.id.radio2);
		/* RadioGroup用OnCheckedChangeListener来执行 */
		mRadioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if (checkedId == mRadio1.getId()) {
					/* 把mRadio1的内容传到mTextView1 */
					toast(mRadio1.getText().toString());
				} else if (checkedId == mRadio2.getId()) {
					/* 把mRadio2的内容传到mTextView1 */
					toast(mRadio2.getText().toString());
				}
			}
		});
		
		button1.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				mRadioGroup1.clearCheck();
			}
			
		});
	}

	public void toast(String str) {
		Toast.makeText(RadioBoxNew.this, str, Toast.LENGTH_LONG).show();
	}
 


RadioBox单选框应用
 
RadioBox单选框应用