Android项目之性别选择

package com.example.androiddemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity {

 private TextView show=null;
 private RadioGroup sex=null;
 private RadioButton male=null;
 private RadioButton female=null;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  this.show=(TextView)super.findViewById(R.id.msg);
  this.sex=(RadioGroup)super.findViewById(R.id.sex);
  this.male=(RadioButton)super.findViewById(R.id.male);
  this.female=(RadioButton)super.findViewById(R.id.female);
  
  this.sex.setOnCheckedChangeListener(new OnCheckedChangeListener() {
   
   @Override
   public void onCheckedChanged(RadioGroup group, int checkedId) {
    // TODO Auto-generated method stub
    String temp=null;
    if(MainActivity.this.male.getId()==checkedId){
     temp=MainActivity.this.male.getText().toString();
    }
    if(MainActivity.this.female.getId()==checkedId){
     temp=MainActivity.this.female.getText().toString();
    }
    MainActivity.this.show.setText("您的性别是:"+temp);
   }
  });
 }
}

 

 

 

效果图:

 

Android项目之性别选择_第1张图片

 

 

 

你可能感兴趣的:(Android项目之性别选择)