android > 控件 > RadioGroup

main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:orientation="vertical"
        android:padding="8dip"
        android:layout_height="wrap_content" >
		    <RadioGroup 
		        android:id="@+id/radioGroup1"
		        android:layout_width="fill_parent"
		        android:layout_height="wrap_content"
		        android:orientation="horizontal"
		        >
		        <RadioButton
		            android:id="@+id/radio1"
		            android:layout_width="wrap_content"
		            android:layout_height="wrap_content"
		            android:checked = "true"
		            android:text="5分钟            " />		        
		        <RadioButton
		            android:id="@+id/radio2"
		            android:layout_width="wrap_content"
		            android:layout_height="wrap_content"
		            android:text="10分钟  " />		        
		    </RadioGroup>
		    <RadioGroup 
		        android:id="@+id/radioGroup2" 
		        android:layout_width="fill_parent"
		        android:layout_height="wrap_content"
		        android:orientation="horizontal"
		        >
		        <RadioButton
		            android:id="@+id/radio3"
		            android:layout_width="wrap_content"
		            android:layout_height="wrap_content"
		            android:text="30分钟          " />		        
		        <RadioButton
		            android:id="@+id/radio4"
		            android:layout_width="wrap_content"
		            android:layout_height="wrap_content"
		            android:text="60分钟  " />		        
		    </RadioGroup>
		    <RadioGroup 
		        android:id="@+id/radioGroup3"
		        android:layout_width="fill_parent"
		        android:layout_height="wrap_content"
		        android:orientation="horizontal"
		        >
		        <RadioButton
		            android:id="@+id/radio5"
		            android:layout_width="wrap_content"
		            android:layout_height="wrap_content"
		            android:text="90分钟          " />		        
		        <RadioButton
		            android:id="@+id/radio6"
		            android:layout_width="wrap_content"
		            android:layout_height="wrap_content"
		            android:text="120分钟" />		        
		    </RadioGroup>
		    <RadioGroup 
		        android:id="@+id/radioGroup4"
		        android:layout_width="fill_parent"
		        android:layout_height="wrap_content"
		        android:orientation="horizontal"
		        >
		        <RadioButton
		            android:id="@+id/radio7"
		            android:layout_width="wrap_content"
		            android:layout_height="wrap_content" />		        
		        <EditText
		            android:id="@+id/edit1"
		            android:layout_width="100dip"
		            android:layout_height="wrap_content" />	
		        <TextView
		            android:id="@+id/text1"
		            android:layout_width="wrap_content"
		            android:text="分钟"
		            android:layout_height="wrap_content" />	
		        	        
		    </RadioGroup>
    </LinearLayout>

</LinearLayout>

 

 

T3Activity.java

 

package t3.com;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class T3Activity extends Activity {
    /** Called when the activity is first created. */

	private RadioGroup radioGroup1;
	private RadioGroup radioGroup2;
	private RadioGroup radioGroup3;
	private RadioGroup radioGroup4;
	private RadioButton radio1;
	private RadioButton radio2;
	private RadioButton radio3;
	private RadioButton radio4;
	private RadioButton radio5;
	private RadioButton radio6;
	private RadioButton radio7;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        radioGroup1 = (RadioGroup)findViewById(R.id.radioGroup1);
        radioGroup2 = (RadioGroup)findViewById(R.id.radioGroup2);
        radioGroup3 = (RadioGroup)findViewById(R.id.radioGroup3);
        radioGroup4 = (RadioGroup)findViewById(R.id.radioGroup4);
        
        radio1 = (RadioButton)findViewById(R.id.radio1);
        radio2 = (RadioButton)findViewById(R.id.radio2);
        radio3 = (RadioButton)findViewById(R.id.radio3);
        radio4 = (RadioButton)findViewById(R.id.radio4);
        radio5 = (RadioButton)findViewById(R.id.radio5);
        radio6 = (RadioButton)findViewById(R.id.radio6);
        radio7 = (RadioButton)findViewById(R.id.radio7);
        

        radioGroup1.setOnCheckedChangeListener(new ClearRadio());
        radioGroup2.setOnCheckedChangeListener(new ClearRadio());
		radioGroup3.setOnCheckedChangeListener(new ClearRadio());
		radioGroup4.setOnCheckedChangeListener(new ClearRadio());
    }
    class ClearRadio implements OnCheckedChangeListener{
    	public void onCheckedChanged(RadioGroup group, int checkedId) {
    		// TODO Auto-generated method stub
            if (group != null) {
                if (group == radioGroup1) {
                    if(checkedId==R.id.radio1||checkedId==R.id.radio2){
                         
                    	radioGroup2.clearCheck();
                    	radioGroup3.clearCheck();
                    	radioGroup4.clearCheck();
                        group.check(checkedId);
                    }
                } else if (group == radioGroup2) {
                    if(checkedId==R.id.radio3||checkedId==R.id.radio4){
                         
                    	radioGroup1.clearCheck();
                    	radioGroup3.clearCheck();
                    	radioGroup4.clearCheck();
                        group.check(checkedId);
                    }
                } else if (group == radioGroup3) {
                	if(checkedId==R.id.radio5||checkedId==R.id.radio6){
                        
                    	radioGroup1.clearCheck();
                    	radioGroup2.clearCheck();
                    	radioGroup4.clearCheck();
                        group.check(checkedId);
                    }
                } else if (group == radioGroup4) {
                	if(checkedId==R.id.radio7){
                        
                    	radioGroup1.clearCheck();
                    	radioGroup2.clearCheck();
                    	radioGroup3.clearCheck();
                        group.check(checkedId);
                    }
                }
                 
            }
            //\\
    	}
    	
    }
    
    public int getRadioId(){
    	int re = 1;
    	if(radio1.isChecked() == true){
			re = 1;
		}
    	if(radio2.isChecked() == true){
			re = 2;
		}
    	if(radio3.isChecked() == true){
			re = 3;
		}
    	if(radio4.isChecked() == true){
			re = 4;
		}
    	if(radio5.isChecked() == true){
			re = 5;
		}
    	if(radio6.isChecked() == true){
			re = 6;
		}
		if(radio7.isChecked() == true){
			re = 7;
		}
		
    	return re;
    }
    public boolean onCreateOptionsMenu(Menu menu) {  
    	 // TODO Auto-generated method stub  
    	 menu.add(0, 1, 1, "1");   
    	 return super.onCreateOptionsMenu(menu);  
    }  
     @Override  
     public boolean onOptionsItemSelected(MenuItem item) {  
    	 // TODO Auto-generated method stub  
    	 if(item.getItemId() == 1){  
    		 setTitle("ID="+getRadioId());
    	 }  
    	 return super.onOptionsItemSelected(item);  
     }  
	//\\
}

 

radio1.setChecked(true);

 

 

你可能感兴趣的:(android)