RadioGroup is an important GUI widget in Android.
Today I learn to use RadioGroup.Now let's go.
First of all,Let us look a picture that shows what's the RadioGroup:
In the layout .xml file,we defined the RadioGroup widget.
the package and class in the src file,we could create a class names RadioTest that to use RadioGroup widget.
public class RadioTest extends Activity {
private RadioGroup genderGroup = null;
private RadioButton femaleButton = null;
private RadioButton maleButton = null;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.radio);
genderGroup=(RadioGroup)findViewById(R.id.genderGroup);
femaleButton=(RadioButton)findViewById(R.id.femaleButton);
maleButton=(RadioButton)findViewById(R.id.maleButton);
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { //监听RadioGroup
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
if(femaleButton.getId() == arg1){
System.out.println("female");
Toast.makeText(RadioTest.this,"female",Toast.LENGTH_SHORT).show(); //定义一个Toast对象并调用show()方法
}
else if(maleButton.getId() == arg1){
System.out.println("male");
}
}
});
}
}
Toast can show some information in android,It doesn't like Dialog,Toast don't have the focus.Cause the Toast only can show a short time,so after some time the Toast will disappear.
Look like this.It's cool,isn't it?