RadioButton is a radio button and multiple RadioButton are placed in a Radiogroup control, which means that only 1 RadioButton can be selected at a time. The checkbox is a multiple-selection button, and the TOATST is a control in Android that displays a hint of a small window message, and the contents of the prompt will automatically disappear after a while.
The Radiogroup and CheckBox controls set the Setoncheckedchangelistener function for the listener, whose input parameter is a function, and within the function to implement 1 internal classes. The input parameters for the Radiogroup listener are Radiogroup.oncheckedchangelistener (), The input parameters for the checkbox listener are the function Compoundbutton.oncheckedchangelistener ().
Development environment: android4.1
The experimental results are as follows (linear layouts are used):
Effect Chart:
The top 3 is a set of Radiogroup, each selected one of the RadioButton, there will be a corresponding hint. And only one of them can be selected.
Each of the following 4 is a checkbox, and you can select more than one of them. Each checkbox is selected or unchecked with a corresponding text hint window.
The code is as follows:
Mainactivity.java:
Copy Code code as follows:
Package com.example.control1;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.CheckBox;
Import Android.widget.CompoundButton;
Import Android.widget.CompoundButton.OnCheckedChangeListener;
Import Android.widget.RadioButton;
Import Android.widget.RadioGroup;
Import Android.widget.TextView;
Import Android.widget.Toast;
public class Mainactivity extends activity {
Define variables for each control
Private TextView who = null;
Private TextView how = NULL;
Private Radiogroup who_group = null;
Private RadioButton--null;
Private RadioButton America = NULL;
Private RadioButton others = null;
Private CheckBox less = null;
Private CheckBox thirty = null;
Private CheckBox forty = null;
Private CheckBox fifty = null;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Get the corresponding control
who = (TextView) Findviewbyid (r.id.who);
how = (TextView) Findviewbyid (r.id.how);
Who_group = (radiogroup) Findviewbyid (R.id.who_group);
(RadioButton) Findviewbyid (R.id.china);
America = (RadioButton) Findviewbyid (R.id.america);
others = (RadioButton) Findviewbyid (r.id.others);
less = (CheckBox) Findviewbyid (r.id.less);
thirty = (CheckBox) Findviewbyid (R.id.thirty);
forty = (CheckBox) Findviewbyid (R.id.forty);
fifty = (CheckBox) Findviewbyid (r.id.fifty);
Setting the listener for Who_group is actually a code whose argument is an object with a refactoring function
Who_group.setoncheckedchangelistener (New Radiogroup.oncheckedchangelistener () {
public void OnCheckedChanged (radiogroup group, int checkedid) {
TODO auto-generated Method Stub
if (Checkedid = = China.getid ()) {
Toast.maketext (Mainactivity.this, "China", Toast.length_short). Show ();
}
else if (Checkedid = = America.getid ()) {
Toast.maketext (Mainactivity.this, "the USA", Toast.length_short). Show ();
}
else if (Checkedid = = Others.getid ()) {
Toast.maketext (Mainactivity.this, "other countries", toast.length_short). Show ();
}
}
});
The following is a listener for 4 checkbox multiple-selection buttons
Less.setoncheckedchangelistener (New Oncheckedchangelistener () {
public void OnCheckedChanged (Compoundbutton buttonview, Boolean ischecked) {
TODO auto-generated Method Stub
if (ischecked)
{
Toast.maketext (Mainactivity.this, "30 below", Toast.length_short). Show ();
}
else{
Toast.maketext (Mainactivity.this, "not 30 below", Toast.length_short). Show ();
}
}
});
The following is a listener for 4 checkbox multiple-selection buttons
Thirty.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {
public void OnCheckedChanged (Compoundbutton buttonview, Boolean ischecked) {
TODO auto-generated Method Stub
if (ischecked)
{
Toast.maketext (Mainactivity.this, "30~39", Toast.length_short). Show ();
}
else{
Toast.maketext (Mainactivity.this, "not 30~39", Toast.length_short). Show ();
}
}
});
The following is a listener for 4 checkbox multiple-selection buttons
Forty.setoncheckedchangelistener (New Oncheckedchangelistener () {
public void OnCheckedChanged (Compoundbutton buttonview, Boolean ischecked) {
TODO auto-generated Method Stub
if (ischecked)
{
Toast.maketext (Mainactivity.this, "40~49", Toast.length_short). Show ();
}
else{
Toast.maketext (Mainactivity.this, "not 40~49", Toast.length_short). Show ();
}
}
});
The following is a listener for 4 checkbox multiple-selection buttons
Fifty.setoncheckedchangelistener (New Oncheckedchangelistener () {
public void OnCheckedChanged (Compoundbutton buttonview, Boolean ischecked) {
TODO auto-generated Method Stub
if (ischecked)
{
Toast.maketext (Mainactivity.this, "more than 50", Toast.length_short). Show ();
}
else{
Toast.maketext (Mainactivity.this, "not more than 50", Toast.length_short). Show ();
}
}
});
}
}
Activity_main.xml:
Copy Code code as follows:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical"
>
<textview
Android:id= "@+id/who"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/who"
/>
<radiogroup
Android:id= "@+id/who_group"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:orientation= "Vertical"
>
<radiobutton
Android:id= "@+id/china"
android:layout_height= "Wrap_content"
Android:layout_width= "Wrap_content"
android:text= "@string/china"
/>
<radiobutton
Android:id= "@+id/america"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/america"
/>
<radiobutton
Android:id= "@+id/others"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/others"
/>
</RadioGroup>
<textview
Android:id= "@+id/how"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/how"
/>
<checkbox
Android:id= "@+id/less"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/less"
/>
<checkbox
Android:id= "@+id/thirty"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/thirty"
/>
<checkbox
Android:id= "@+id/forty"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/forty"
/>
<checkbox
Android:id= "@+id/fifty"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/fifty"
/>
</LinearLayout>
The experiment concludes: This experiment has a preliminary understanding of the simple use of the 4 controls, Radiogroup,checkbox,radiobutton and toast.
Author: tornadomeet