Android development path (9) radiobox, checkbox, and Spinner

Source: Internet
Author: User

Reprinted please indicate this article from "LIU Da's csdn blog": http://blog.csdn.net/Poechant

1. radiobox and radiogroup

In the article "android development path (5) widgets, activity and intent", we have initially come into use with radiobox, it is also known that radiogroup is an essential container for using radiobox In the widget's accommodation relationship. This blog is not in-depth
For more information about how to use widgets or analyze the source code, but only familiar with common widgets in Android, I will not go into details here :)

2. checkbox

The difference between checkbox and radiobox is that it can be checked. The following is an example:

Final textview = (textview) findviewbyid (R. Id. textview );

Final checkbox [] checkboxes = {

(Checkbox) findviewbyid (R. Id. checkbox01 ),

(Checkbox) findviewbyid (R. Id. checkbox02 ),

(Checkbox) findviewbyid (R. Id. checkbox03 ),

(Checkbox) findviewbyid (R. Id. checkbox04 ),

(Checkbox) findviewbyid (R. Id. checkbox05 ),

(Checkbox) findviewbyid (R. Id. checkbox06 ),

(Checkbox) findviewbyid (R. Id. checkbox07 ),

(Checkbox) findviewbyid (R. Id. checkbox08 ),

(Checkbox) findviewbyid (R. Id. checkbox09)

};

For (final checkbox: checkboxes ){

Checkbox. setoncheckedchangelistener (New oncheckedchangelistener (){

@ Override

Public
Void oncheckedchanged (compoundbutton buttonview,
Boolean ischecked ){

Textview. settext ("You have chose" + checkbox. gettext () + ".");

}

});

}


Checkbox01 and checkbox09 are all defined in the layout file. Create an event listener for each checkbox through a loop body. In this way, the textview above is displayed immediately when each option is selected. The following figure shows the program startup.

After selecting the option "Korea", note that the above textview has not changed?

3. Spinner

The spinner is a drop-down list. The following example shows how to create a spinner and display it in textview after selecting an item.

Public class testextends activity {

Private staticfinal string []
Countries = new string [] {

"Argentina", "Australia ",
"Brazil", "Canada ",
"China", "China Hongkong ",

"China Macau", "China Taiwan ",
"Egypt", "Finland ",
"France ",

"Germany", "India ",
"Japan", "Korea ",
"South Africa", "Russia ",
"UK", "us"

};

Public void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Main );

// Get widgets
IDS

Final textview = (textview) findviewbyid (R. Id. textview );

Spinner spinner = (spinner) findviewbyid (R. Id. spinner );

// Create an adapter filled with countries array for the spinner

Arrayadapter <string> adapter = new arrayadapter <string> (this,

Android. R. layout. simple_spinner_item,
Countries );

Adapter. setdropdownviewresource (Android. R. layout. simple_spinner_dropdown_item );

// Combine the adapter with the spinner

Spinner. setadapter (adapter );

Spinner. setonitemselectedlistener (new Spinner. onitemselectedlistener (){

@ Override

Public
Void onitemselected (adapterview <?> Arg0, view arg1,

Int arg2,
Long arg3 ){

Textview. settext ("I'm in" +
Countries [arg2] + "now .");

Arg0.setvisibility (view. Visible );

}

@ Override

Public
Void onnothingselected (adapterview <?> Arg0 ){

//
Todo auto-generated method stub

}

});

}

}


First, create an arrayadapter <string> type because countries is a string array. Set the adapter of the spinner in the drop-down list to it. Then add the onitemselectedlistener listener for it, where the override onitemselected method and onnothingselected method. After the program is started, the following information is displayed:

The options-related widgets are first recognized here :)

Reprinted please indicate this article from "LIU Da's csdn blog": http://blog.csdn.net/Poechant

-

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.