Android control development Radio (radio button) and checkbox (Multi-select button) development

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.