首页 > 代码库 > 熟悉AndroidAPI系列3——RadioGroup and RaioButton

熟悉AndroidAPI系列3——RadioGroup and RaioButton

技术分享

选择A,会同时选中A,C

选择B,会同时选中A,C

 

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >        <RadioGroup         android:id="@+id/radioGrp1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal">                <RadioButton             android:id="@+id/aId"            android:layout_width = "wrap_content"            android:layout_height = "wrap_content"            android:text = "A"/>        <RadioButton             android:id="@+id/bId"            android:layout_width = "wrap_content"            android:layout_height = "wrap_content"            android:text = "B"/>        </RadioGroup><RadioGroup        android:id="@+id/radioGrp2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <RadioButton            android:id="@+id/cId"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text = "C" />        <RadioButton            android:id="@+id/dId"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="D" />    </RadioGroup>    </LinearLayout>
package com.njulya.testradiobutton;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;public class MainActivity extends Activity {     private RadioGroup radioGrp1;     private RadioGroup radioGrp2;     private RadioButton aButton;     private RadioButton bButton;     private RadioButton cButton;     private RadioButton dButton;         @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_1);                radioGrp1 = (RadioGroup)findViewById(R.id.radioGrp1);        radioGrp2 = (RadioGroup)findViewById(R.id.radioGrp2);        aButton = (RadioButton)findViewById(R.id.aId);        bButton = (RadioButton)findViewById(R.id.bId);        cButton = (RadioButton)findViewById(R.id.cId);        dButton = (RadioButton)findViewById(R.id.dId);                RadioGroupListener rGrpListener = new RadioGroupListener();                radioGrp1.setOnCheckedChangeListener(rGrpListener);        radioGrp2.setOnCheckedChangeListener(rGrpListener);            }    //import android.widget.RadioGroup.OnCheckedChangeListener;    private class RadioGroupListener implements OnCheckedChangeListener{        @Override        public void onCheckedChanged(RadioGroup radioGrp, int checkedId) {            if(checkedId == aButton.getId()){                cButton.setChecked(true);            }else if(checkedId == bButton.getId()){                dButton.setChecked(true);            }else if(checkedId == cButton.getId()){                aButton.setChecked(true);            }else if(checkedId == dButton.getId()){                bButton.setChecked(true);            }        }            }        @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    }

 

熟悉AndroidAPI系列3——RadioGroup and RaioButton