码迷,mamicode.com
首页 > 移动开发 > 详细

熟悉AndroidAPI系列3——RadioGroup and RaioButton

时间:2014-12-27 12:35:46      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

选择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

标签:

原文地址:http://www.cnblogs.com/lya-nju/p/4188168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!