RadioGroup and RadioButton controls
First, you need to set the RadioGroup attribute in the layout file, and then add the RadioButton attribute to the RadioGroup. It can also be considered that RadioGroup is a RadioButton container. First, create a container and then add an object to the container.
The code snippet is as follows:
<RadioGroup
Android: id = "@ + id/gender" // set the id
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "vertical" // sets the direction of RadioButton in RadioGroup.
>
<RadioButton
Android: id = "@ + id/male" // set the id
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/male"
/>
<RadioButton
Android: id = "@ + id/female" // sets the id
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/female"
/>
</RadioGroup>
After the layout file is written, add the RadioGroup and RadioButton objects to the Activity class.
This control event needs to be set to RadioGroup. You can use setOnCheckedChangeListener of RadioGroup to add the listener of OnCheckedChangeListener of RadioGroup. onCheckedChanged (RadioGroup group, int checkedId) method, the parameter is obvious, is the group object and the changed RadioButton id, In the method can perform operations on RadioGroup and RadioButton.
The code snippet is as follows:
RadioGroup genderGroup = (RadioGroup) findViewById (R. id. gender );
RadioButton maleButton = (RadioButton) findViewById (R. id. male );
MaleButton. setChecked (true); // male is selected by default.
RadioButton femaleButton = (RadioButton) findViewById (R. id. female );
GenderGroup. setOnCheckedChangeListener (new RadioGroup. OnCheckedChangeListener (){
@ Override
Public void onCheckedChanged (RadioGroup group, int checkedId ){
// TODO Auto-generated method stub
If (femaleButton. getId () = checkedId ){
Toast. makeText (ControlDemo2Activity. this, "Female", Toast. LENGTH_SHORT). show ();
}
Else {
Toast. makeText (ControlDemo2Activity. this, "Male", Toast. LENGTH_SHORT). show ();
}
}
});
Running effect:
CheckBox control
First, define the CheckBox style id and other information in the layout file. Each CheckBox needs to be defined.
The code snippet is as follows:
<CheckBox
Android: id = "@ + id/apple"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/apple"
/>
<CheckBox
Android: id = "@ + id/orange"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/orange"
/>
<CheckBox
Android: id = "@ + id/mango"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/mango"
/>
After defining the CheckBox, create a CheckBox object in the Activity. You can set listeners for each CheckBox. You can use the setOnCheckedChangeListener method of the CheckBox to add the OnCheckedChangeListener listener of CompoundButton, onCheckedChanged (CompoundButton arg0, boolean arg1) in Override. The first parameter is the object of the CheckBox you clicked, and the second parameter is the selected boolean value of the object, you can define some function effects in the method.
The code snippet is as follows:
CheckBox appleCheck = (CheckBox) findViewById (R. id. apple );
AppleCheck. setOnCheckedChangeListener (new CompoundButton. OnCheckedChangeListener (){
@ Override
Public void onCheckedChanged (CompoundButton arg0, boolean arg1 ){
// TODO Auto-generated method stub
If (arg1 = true) // when the Checkbox is pressed
Toast. makeText (ControlDemo2Activity. this, "Apple Checked", Toast. LENGTH_SHORT). show ();
Else
Toast. makeText (ControlDemo2Activity. this, "Apple Unchecked", Toast. LENGTH_SHORT). show ();
}
});
Running effect:
The style of the above two controls can be set in the layout file or object.
The attachment is the sample code for your reference only. Ga ~
This article is from the "Temple of war" blog