Android control development Radio (radio button) and checkbox (Multi-select button) development
This blog is mainly about the use of radio and multi-select buttons in Android development, see example code:
Mainactivity.java:
Package com.example.radiotest;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.widget.CheckBox;
Import Android.widget.CompoundButton;
Import Android.widget.RadioButton;
Import Android.widget.RadioGroup;
Import Android.widget.Toast;
public class Mainactivity extends Activity {
Private Radiogroup genergroup = null;
Private RadioButton Malebutton = null;
Private RadioButton Femalebutton =null;
Private CheckBox Swin = null, run = NULL, jump = NULL;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Genergroup = (radiogroup) Findviewbyid (R.id.mysexgroup);
Malebutton = (RadioButton) Findviewbyid (R.id.malebutton);
Femalebutton = (RadioButton) Findviewbyid (R.id.femalebutton);
Swin = (CheckBox) Findviewbyid (R.id.swinbox);
Run = (CheckBox) Findviewbyid (R.id.runbox);
Jump = (CheckBox) Findviewbyid (R.id.jumpbox);
Setting up the Radiogroup listener
Genergroup.setoncheckedchangelistener (New Radiogroup.oncheckedchangelistener () {
@Override
public void OnCheckedChanged (radiogroup group, int checkedid) {
TODO auto-generated Method Stub
if (Checkedid = = Malebutton.getid ()) {
Toast.maketext (Mainactivity.this, "male", Toast.length_short). Show ();
}
else if (Checkedid = = Femalebutton.getid ()) {
Toast.maketext (Mainactivity.this, "female", Toast.length_short). Show ();
}
}
});
To set the listener for a checkbox multiple selection
Swin.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {
@Override
public void OnCheckedChanged (Compoundbutton buttonview, Boolean isChecked) {
TODO auto-generated Method Stub
if (isChecked) {
System.out.println ("Swin is checked!");
}
else{
System.out.println ("Swin is unchecked!");
}
}
});
Run.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {
@Override
public void OnCheckedChanged (Compoundbutton buttonview, Boolean isChecked) {
TODO auto-generated Method Stub
if (isChecked) {
System.out.println ("Run is checked!");
}
else{
System.out.println ("Run is unchecked!");
}
}
});
Jump.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {
@Override
public void OnCheckedChanged (Compoundbutton buttonview, Boolean isChecked) {
TODO auto-generated Method Stub
if (isChecked) {
System.out.println ("Jump is checked!");
}
else{
System.out.println ("Jump is unchecked!");
}
}
});
}
@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;
}
}
layout file Main.xml:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:id= "@+id/linearlayout1"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical"
Tools:context= ". Mainactivity ">
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>
<radiogroup
Android:id= "@+id/mysexgroup"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
>
<radiobutton
Android:id= "@+id/malebutton"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/male"/>
<radiobutton
Android:id= "@+id/femalebutton"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/female"/>
</RadioGroup>
<checkbox
Android:id= "@+id/swinbox"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/swin"/>
<checkbox
Android:id= "@+id/runbox"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/run"/>
<checkbox
Android:id= "@+id/jumpbox"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/jump"/>
</linearlayout>
The
effect is as follows:
Android control development Radio (radio button) and checkbox (Multi-select button) development