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:

<? 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:

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; P Rivate radiobutton shaseng;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // find main by ID. textview control text = (textview) findviewbyid (R. id. text); // Tang Miao Shan javastangseng = (radiobutton) findviewbyid (R. id. tangseng); // Wukong ticket javaswukong = (radiobutton) findviewbyid (R. id. wukong); // Bajie = (Radiobutton) findviewbyid (R. id. bajie); // sashan Shan mongoshaseng = (radiobutton) findviewbyid (R. id. shaseng); // find main by ID. button button = (button) findviewbyid (R. id. button); // Add and click the listener button for the button control. setonclicklistener (this);} private void updatetext (string) {// set the text information to the textview control to display text. settext (string) ;}@ overridepublic void onclick (view v) {string STR = ""; // If (tangseng. ischecked () {s Tr + = "Tang Seng ~ ";}// If (Wukong. ischecked () {STR + =" Wukong ~ ";}// If (Bajie. ischecked () {STR + =" ~ ";}// If (shaseng. ischecked () {STR + =" sashan ~ ";}// No one is selected if (Str. Equals (" ") {STR + =" no one ";} 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:

<? 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:

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 check Box shaseng;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // find main by ID. textview control text = (textview) findviewbyid (R. id. text); // check box tangseng = (checkbox) findviewbyid (R. id. tangseng); // check box Wukong = (checkbox) findviewbyid (R. id. wukong); // check box Bajie = (checkbox) findviewbyid (R. id. bajie); // Sha Seng check box shaseng = (checkbox) findviewbyid (R. id. shaseng); // find main by ID. button button = (button) findviewbyid (R. id. button); // Add and click the listener button for the button control. setonclicklistener (this);} private void updatetext (string) {// set the text information to the textview control to display text. settext (string) ;}@ overridepublic void onclick (view v) {string STR = ""; // The if (tangseng. ischecked () {STR + = "Tang Seng ~ ";}// Check box of Wukong is selected if (Wukong. ischecked () {STR + =" Wukong ~ ";} // Select the" If (Bajie. ischecked () "{STR + =" ~ ";}// Select the Sha Seng check box if (shaseng. ischecked () {STR + =" Sha Seng ~ ";}// No one is selected if (Str. Equals (" ") {STR + =" no one ";} 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 watching the Journey to the West? Someone else saves the money.

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.