RadioButton and CheckBox from the ground up

Source: Internet
Author: User

RadioButton
 
Class Structure:


 
RadioButton is a good understanding of the single-choice button in Web development. It should be noted that we need to set the same name for the single-choice button in Web development. Android development here, we need to put two or more RadioButton in a RadioGroup.
 
Practical drills:
1. How to set the alignment of RadioButton:
Android: orientation has the following values: vertical and horizontal.
Vertical:

<RadioGroup android: id = "@ + id/raGroup01"
 
Android: layout_width = "fill_parent" android: layout_height = "wrap_content"
 
Android: orientation = "vertical">
 
<RadioButton android: id = "@ + id/raBtn01" android: text = "male"/>
 
<RadioButton android: id = "@ + id/raBtn02" android: text = ""/>
 
</RadioGroup>

Effect:

Level:

<RadioGroup android: id = "@ + id/raGroup02"
 
Android: layout_width = "fill_parent" android: layout_height = "wrap_content"
 
Android: orientation = "horizontal">
 
<RadioButton android: id = "@ + id/rautf" android: text = "UTF"/>
 
<RadioButton android: id = "@ + id/ragbk" android: text = "GBK"/>
 
</RadioGroup>

Effect:

2. How to Set RadioButton to select
Android: checkedButton = "@ + id/rautf"

<RadioGroup android: id = "@ + id/raGroup02"
 
Android: layout_width = "fill_parent" android: layout_height = "wrap_content"
 
Android: orientation = "horizontal"
 
Android: checkedButton = "@ + id/rautf">
 
<RadioButton android: id = "@ + id/rautf" android: text = "UTF"/>
 
<RadioButton android: id = "@ + id/ragbk" android: text = "GBK"/>
 
</RadioGroup>

 
Effect:
 
Add listener:

RadioButton01 = (RadioButton) findViewById (R. id. raBtn01 );
 

 
RadioButton01.setOnClickListener (new OnClickListener (){
 
Public void onClick (View v ){
 
String text = radioButton01.getText (). toString ();
 
System. out. println (text );
 

 
}
 
});
 

 
 
CheckBox
 
Class Structure:

 
Instance:

<TextView android: layout_width = "fill_parent"
 
Android: layout_height = "wrap_content" android: text = "hobbies"/>
 

 
<CheckBox android: id = "@ + id/chckBox01" android: text = "Sports"
 
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"/>
 

 
<CheckBox android: id = "@ + id/chckBox02" android: text = "Literature"
 
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"
 
Android: checked = "true"/>
 

Effect:
 
Add listener registration event for CheckBox

CheckBox01.setOnClickListener (new OnClickListener (){
 
 
 
@ Override
 
Public void onClick (View v ){
 
System. out. println (checkBox01.getText (). toString ());
 
 
 
}
 
});

From: jiahui524 Column

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.