Android [elementary tutorial] Article 3 RadioButton and CheckBox controls

Source: Internet
Author: User

This time we will talk about the RadioButton and CheckBox controls. First, we will talk about the RadioButton controls.

I believe that you will surely have read the Journey to the West. There is a scenario where the goblin captures Tang Miao. We will use these two controls to simulate the RadionButton control. It means that each time a goblin captures only one person, you have to catch a few times. This can make the goblin busy.

Let's take a look at the code in main. xml:

View plain
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<RadioGroup android: id = "@ + id/radioGroup1"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content">
<RadioButton android: layout_height = "wrap_content"
Android: layout_width = "wrap_content" android: text = "Tang Seng" android: id = "@ + id/tangseng"/>
<RadioButton android: id = "@ + id/wukong" android: text = "Sun wukong"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"/>
<RadioButton android: layout_height = "wrap_content"
Android: layout_width = "wrap_content" android: text = "" android: id = "@ + id/bajie"/>
<RadioButton android: layout_height = "wrap_content"
Android: layout_width = "wrap_content" android: text = "Sha Monk" android: id = "@ + id/shaseng"/>
</RadioGroup>
 
<Button android: layout_width = "match_parent"
Android: layout_height = "wrap_content" android: text = "button" android: id = "@ + id/Button"> </button>
<TextView android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: text = "@ string/hello"
Android: id = "@ + id/text"> </TextView>
</LinearLayout>
The java code in the Activity is as follows:

View plain
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. RadioButton;
Import android. widget. TextView;
 
Public class ButtonDemoActivity extends Activity implements OnClickListener
{
Private TextView text = null;
Private RadioButton tangseng;
Private RadioButton wukong;
Private RadioButton bajie;
Private RadioButton shaseng;
 
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main); www.2cto.com
 
// Find the TextView control in main. xml by ID
Text = (TextView) findViewById (R. id. text );

// Tang Shanshan
Tangseng = (RadioButton) findViewById (R. id. tangseng );
// Wukong ticket
Wukong = (RadioButton) findViewById (R. id. wukong );
// Eight-ring Singles Day
Bajie = (RadioButton) findViewById (R. id. bajie );
// Shanshan Shan
Shaseng = (RadioButton) findViewById (R. id. shaseng );
 
// Find the Button control in main. xml by ID
Button button = (Button) findViewById (R. id. button );
// Add a click listener for the Button control
Button. setOnClickListener (this );
 
}
 
 
Private void updateText (String string)
{
// Set the text information to display in the TextView control.
Text. setText (string );
}
 
 
@ Override
Public void onClick (View v)
{
String str = "";
// Tang Seng Singles' Day selected
If (tangseng. isChecked ()){
Str + = "Tang Seng ~ ";
}
// Wukong ticket selected
If (wukong. isChecked ()){
Str + = "Wukong ~ ";
}
// Select the eight-ring Singles Day
If (bajie. isChecked ()){
Str + = "eight rings ~ ";
}
// Sashena Singleton is selected
If (shaseng. isChecked ()){
Str + = "sashan ~ ";
}
// No one is selected
If (str. equals ("")){
Str + = "nobody ";
}
Str + = "the goblin has been taken away! ";

UpdateText (str );

}
 
}
 

Every time I capture only one thing, it's a little too much trouble. I have a high skill. I have to catch a few more at a time, which saves a lot of trouble. Now, we have used the CheckBox control.

The main. xml Code is as follows:

View plain
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical" android: layout_width = "fill_parent"
Android: layout_height = "fill_parent">
<CheckBox android: text = "Tang Seng" android: id = "@ + id/tangseng"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </CheckBox>
<CheckBox android: text = "Sun wukong" android: id = "@ + id/wukong"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </CheckBox>
<CheckBox android: text = "" android: id = "@ + id/bajie"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </CheckBox>
<CheckBox android: text = "Sha Monk" android: id = "@ + id/shaseng"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </CheckBox>
 
<Button android: layout_width = "match_parent"
Android: layout_height = "wrap_content" android: text = "button" android: id = "@ + id/Button"> </button>
<TextView android: layout_height = "wrap_content"
Android: layout_width = "fill_parent" android: text = "@ string/hello"
Android: id = "@ + id/text"> </TextView>
</LinearLayout>

The java code in the Activity is as follows:

View plain
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. CheckBox;
Import android. widget. TextView;
 
Public class ButtonDemoActivity extends Activity implements OnClickListener
{
Private TextView text = null;
Private CheckBox tangseng;
Private CheckBox wukong;
Private CheckBox bajie;
Private CheckBox shaseng;
 
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
 
// Find the TextView control in main. xml by ID
Text = (TextView) findViewById (R. id. text );

// Check box for Tang Seng
Tangseng = (CheckBox) findViewById (R. id. tangseng );
// Check box for Wukong
Wukong = (CheckBox) findViewById (R. id. wukong );
// Check box for the eight rings
Bajie = (CheckBox) findViewById (R. id. bajie );
// Sha Seng check box
Shaseng = (CheckBox) findViewById (R. id. shaseng );
 
// Find the Button control in main. xml by ID
Button button = (Button) findViewById (R. id. button );
// Add a click listener for the Button control
Button. setOnClickListener (this );
 
}
 
 
Private void updateText (String string)
{
// Set the text information to display in the TextView control.
Text. setText (string );
}
 
 
@ Override
Public void onClick (View v)
{
String str = "";
// Check box selected
If (tangseng. isChecked ()){
Str + = "Tang Seng ~ ";
}
// The Wukong check box is selected.
If (wukong. isChecked ()){
Str + = "Wukong ~ ";
}
// Select the eight-ring check box
If (bajie. isChecked ()){
Str + = "eight rings ~ ";
}
// Select the Sha Seng check box
If (shaseng. isChecked ()){
Str + = "sashan ~ ";
}
// No one is selected
If (str. equals ("")){
Str + = "nobody ";
}
Str + = "the goblin has been taken away! ";

UpdateText (str );

}
 
}
Haha, are you happy to take all four at a time? What? Four of them have been taken away. Who can save Tang Miao? Don't worry, don't you even get the Goblin to eat at the end of your journey to the West? Someone else can help you.

 

From: kangkangz4 Column

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.