熟悉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;
    }
    
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。