Android Learning Notes dialog Box collection

Source: Internet
Author: User

 PackageCom.zhangbz.dialog;Importandroid.app.Activity;ImportAndroid.app.AlertDialog;ImportAndroid.app.AlertDialog.Builder;ImportAndroid.app.ProgressDialog;ImportAndroid.content.DialogInterface;ImportAndroid.content.DialogInterface.OnClickListener;ImportAndroid.content.DialogInterface.OnMultiChoiceClickListener;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }     Public voidclick1 (view view) {//the creator of the dialog boxAlertdialog.builder Builder =NewBuilder ( This); Builder.settitle ("I am a dialog box"); Builder.setmessage (The dialog box displays the contents of the); Builder.setpositivebutton ("OK",NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface arg0,intarg1) {Toast.maketext (Getapplicationcontext (),"OK was clicked", 0). Show ();        }        }); Builder.setnegativebutton ("Cancel",NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface arg0,intarg1) {                //do not write anything, the default implementation is to close down the dialog box            }        }); Builder.setcancelable (false);//settings cannot be canceled//builder.create (). Show ();builder.show (); }    /*** Single-selection dialog box *@paramView*/     Public voidClick2 (view view) {//dialog Box CreatorBuilder Builder =NewBuilder ( This); Builder.settitle ("Please select your gender"); Finalstring[] items = {"Male", "female"}; Builder.setsinglechoiceitems (Items,1,NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Toast.maketext (Getapplicationcontext (),"Your gender is:" + Items[which], 0). Show ();            Dialog.dismiss ();        }        });    Builder.show (); }    /*** Multi-Select dialog box *@paramView*/     Public voidClick3 (view view) {//dialog Box CreatorBuilder Builder =NewBuilder ( This); Builder.settitle ("Please choose your favorite fruit"); Finalstring[] items = {"Apple", "pear", "pineapple", "banana"}; Final  Boolean[] result =New Boolean[]{true,false,true,false}; Builder.setmultichoiceitems (items, result,NewOnmultichoiceclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intWhich,Booleanischecked) {Toast.maketext (Getapplicationcontext (), Items[which]+ ischecked, 0). Show (); Result[which]=ischecked;        }        }); Builder.setpositivebutton ("Submit",NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface arg0,intarg1) {StringBuffer SB=NewStringBuffer ();  for(inti = 0; i < result.length; i++) {                    if(Result[i]) {sb.append (Items[i]+ ","); }} toast.maketext (Getapplicationcontext (),"You have selected" + sb.tostring (), 0). Show ();        }        });            Builder.show (); }    //Progress Bar dialog box     Public voidClick4 (view view) {ProgressDialog PD=NewProgressDialog ( This); Pd.settitle (Remind); Pd.setmessage ("Loading Data ... Please wait a moment. ");    Pd.show (); }    //dialog box with progress bar     Public voidclick5 (view view) {FinalProgressDialog PD =NewProgressDialog ( This); Pd.settitle (Remind);        Pd.setprogressstyle (progressdialog.style_horizontal); Pd.setmax (100); Pd.setmessage ("Loading Data ... Just a moment, please.);        Pd.show (); NewThread () { Public voidrun () { for(inti = 0; I < 100; i++) {                    Try{Thread.Sleep (40); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace ();                } pd.setprogress (i);            } Pd.dismiss ();        };    }.start (); }}

Android Learning Notes dialog Box collection

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.