Android - RadioButton

摘要:Android - RadioButton

xml


					<RadioGroup 
					    android:layout_width="match_parent"
					    android:layout_height="wrap_content"
					    android:orientation="horizontal"
					    android:gravity="center">
					    <RadioButton android:id="@+id/radio_yes"
					        android:layout_width="wrap_content"
					        android:layout_height="wrap_content"
					        android:text="@string/yes"
					        android:textAppearance="?android:attr/textAppearanceMedium"		/>
					    <RadioButton android:id="@+id/radio_no"
					        android:layout_width="wrap_content"
					        android:layout_height="wrap_content"
					        android:text="@string/no"
					        android:textAppearance="?android:attr/textAppearanceMedium"		/>
					</RadioGroup>

 

 

java


        mRadioYes = (RadioButton)findViewById(R.id.radio_yes);
        mRadioNo = (RadioButton)findViewById(R.id.radio_no);
        mRadioYes.setOnClickListener(mRadioOnClick);
        mRadioNo.setOnClickListener(mRadioOnClick);

 


    private OnClickListener mRadioOnClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean checked = ((RadioButton) v).isChecked();
            
            switch(v.getId()) {
                case R.id.radio_yes:
                    //do yes
                    break;
                case R.id.radio_no:
                   //do no
                    break;
            }
        }
    };