RadioButton练习(android)

Radio 单选是由 RadioGroup 与 RadioButton 组成的,这个很简单,看代码就会了

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"   >

     < TextView
        
android:layout_width ="fill_parent"
        android:layout_height
="wrap_content"
        android:text
="你的性别:请选择答案 "   />
     < RadioGroup 
        
android:id ="@+id/rg1"
        android:layout_width
="wrap_content"
        android:layout_height
="wrap_content"
        android:orientation
="vertical"
        android:layout_x 
= "3px"
        android:layout_y 
= "54px"
        
>
         < RadioButton
            
android:id ="@+id/rb1"
            android:layout_width
="wrap_content"
            android:layout_height
="wrap_content"
            android:text
="男生"
             
/>
          < RadioButton
            
android:id ="@+id/rb2"
            android:layout_width
="wrap_content"
            android:layout_height
="wrap_content"
            android:text
="女生"
             
/>
     RadioGroup >
     < Button 
        
android:id ="@+id/btn"
        android:layout_width
="wrap_content"
        android:layout_height
="wrap_content"
        android:text
="测试"
        
/>
LinearLayout >

 

代码

 

package zziss.android.radiotest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public  class RadioTestActivity  extends Activity {
     /**  Called when the activity is first created.  */
    RadioGroup rg1 ;
    RadioButton rb1;
    RadioButton rb2;
    Button      ibtn;
    @Override
     public  void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        rg1 = (RadioGroup) this.findViewById(R.id.rg1);
        rb1 = (RadioButton) this.findViewById(R.id.rb1);
        rb2 = (RadioButton) this.findViewById(R.id.rb2);
        
        rg1.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() {
            
            @Override
             public  void onCheckedChanged(RadioGroup group,  int checkedId) {
                 //  TODO Auto-generated method stub
                 if (checkedId == rb1.getId())
                {
                    ShowInfo(rb1.getText().toString());
                }
                 if (checkedId == rb2.getId())
                {
                    ShowInfo(rb2.getText().toString());
                }
            }
        });
        
        ibtn = (Button) this.findViewById(R.id.btn);
        ibtn.setOnClickListener( new View.OnClickListener() {
            
            @Override
             public  void onClick(View v) {
                 //  TODO Auto-generated method stub
                 int id = rg1.getCheckedRadioButtonId();
                 if (id != -1)
                    showIdText(id);
                 else
                    ShowInfo("请选择");
            }
        });
    }
    
     private  void ShowInfo(String str)
    {
        Toast toast = Toast.makeText( this, str, Toast.LENGTH_SHORT);
        toast.show();
    }
     private  void showIdText( int aId)
    {
        ShowInfo(((RadioButton) this.findViewById(aId)).getText().toString());
    }
    
}

 

转载于:https://www.cnblogs.com/zziss/archive/2011/12/14/2288220.html

你可能感兴趣的:(RadioButton练习(android))