Android radio List dialog box builder. setsinglechoiceitems

Source: Internet
Author: User
Import android. widget. button; import android. widget. toast; public class singlechoiceitemstest extends activity implements onclicklistener {private string [] province = new string [] {"Shanghai", "Beijing", "Hunan", "Hubei ", "Hainan"}; private button btnsinglechoicelist; // click the instance of the event object private buttononclick = new buttononclick (1);/** called when the activity is first created. * // @ override public void oncreat E (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); btnsinglechoicelist = (button) findviewbyid (R. id. btnsinglechoice); btnsinglechoicelist. setonclicklistener (this) ;}@ override public void onclick (view v) {showsinglechoicebutton ();} // display the OK and cancel buttons in the single-choice options. // the Data Type of the buttononclickg variable is buttononclick. One click event class private void showsinglechoicebutton () {alertdialog. B Uilder builder = new alertdialog. builder (this); builder. settitle ("select Province"); builder. setsinglechoiceitems (province, 0, buttononclick); builder. setpositivebutton ("OK", buttononclick); builder. setnegativebutton ("cancel", buttononclick); builder. show ();} private class buttononclick implements dialoginterface. onclicklistener {private int index; // indicates the option index public buttononclick (INT index) {This. index = ind Ex ;}@ override public void onclick (dialoginterface diich, int which) {// which indicates the clicked button index. All option indexes are greater than 0, and the button indexes are smaller than 0. If (which> = 0) {// if you click a list item, save the index of the current list item in the index. // To close the dialog box after clicking a list item, call dialog. Cancel () // or use the dialog. Dismiss () method. Index = which;} else {// the user clicks the [OK] button if (which = dialoginterface. button_positive) {// displays the list items selected by the user. Final alertdialog ad = new alertdialog. builder (singlechoiceitemstest. this ). setmessage ("the region you selected is:" + index + ":" + province [Index]). show (); // It is automatically disabled in five seconds. Handler hander = new handler (); runnable = new runnable () {@ override public void run () {ad. dismiss () ;}}; hander. postdelayed (runnable, 5*1000);} // the user clicks the cancel button else if (which = dialoginterface. button_negative) {toast. maketext (singlechoiceitemstest. this, "You have not selected anything", toast. length_long );}}}}}

Alertdialog. the setsinglechoiceitems method of the builder class can create a list with single buttons: Method: 1. load data from the resource file: Public builder setsinglechoiceitems (INT itemsid, int checkeditem, final onclicklistener listener) 2. load data from the dataset public builder setsinglechoiceitems (cursor, int checkeditem, string
Labelcolumn, final onclicklistener listener) 3. load data from the string array public builder setsinglechoiceitems (charsequence [] items, int checkeditem, final onclicklistener listener) 4. Load the public builder setsinglechoiceitems (listadapter adpater, int checkeditem,
Final onclicklistener listener)

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.