"Reading notes-the zero-start of Android game programming" 7.Android game development Common system controls (Dialog)

Source: Internet
Author: User

In Android app Development, Dialog (dialog box) is created simple and easy to manage and is often used, and dialog box default style is similar to creating style activity.
The class of builder under Android.app.AlertDialog is introduced first. Builder is a subclass of the Alertdialog class, and it is also its inner class. As the name implies, builder is the equivalent of a concrete constructor, which sets the dialog property through builder and then displays the Builder (dialog box).

I made a dialog display effect set of small demo, the effect is as follows (GIF picture is larger, need point load time):

Main code:

 Packageyc.example.dialogshow;Importandroid.app.Activity;ImportAndroid.app.AlertDialog;ImportAndroid.app.Dialog;ImportAndroid.app.ProgressDialog;ImportAndroid.content.DialogInterface;ImportAndroid.content.DialogInterface.OnClickListener;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {Private StaticAlertdialog.builder Builder; Private StaticDialog Dialog; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }     Public voidBtn1_onclick (view view) {//Instantiate builderBuilder =NewAlertdialog.builder (mainactivity. This); //Setting the icon for a dialog box//Builder.seticon (r.drawable.ic_launcher);Builder.seticon (Android.        R.drawable.ic_dialog_info); //Setting the icon for a dialog boxBuilder.settitle ("dialog box title"); //set the prompt text for a dialog boxBuilder.setmessage ("Prompt Text for dialog box"); Builder.setpositivebutton ("OK",NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface arg0,intarg1) {Toast.maketext (mainactivity). This, "This is a simple dialog box", Toast.length_short). Show ();        }        }); //call the Show () method to display the dialog boxbuilder.show (); }     Public voidBtn2_onclick (View view) {dialog=NewAlertdialog.builder ( This). SetIcon (R.drawable.ic_launcher). Settitle ("Hint"). Setmessage ("Confirm exit?"). "). Setpositivebutton ("OK",NULL). Setnegativebutton ("Cancel",NULL). Show (); }     Public voidBtn3_onclick (View view) {dialog=NewAlertdialog.builder ( This). SetIcon (Android. R.drawable.btn_star). Settitle ("Preferences Survey"). Setmessage ("Do you like to play lol?" "). Setpositivebutton (" Very like ",NULL)//monitor P-type button, positive polarity;. Setnegativebutton ("Don't like",NULL)//Monitor N-type button, negative polarity;. Setneutralbutton ("General",NULL)//Monitor the neutral button, usually in the middle. Show (); }     Public voidBtn4_onclick (View view) {dialog=NewAlertdialog.builder ( This). SetIcon (Android. R.drawable.ic_dialog_info). Settitle ("Please enter")                /** The Setview () method is to add a system component to the dialog box, but only one component can be set, and if used more than once, the first set of components will be replaced by the post-set component * Setview () Add a system component layout, default At the bottom of the dialog box (above the button)*/. Setview (NewEditText ( This). Setpositivebutton ("OK",NULL). Setnegativebutton ("Cancel",NULL). Show (); }     Public voidBtn5_onclick (view view) {Finalstring[] Wujiang =NewString[] {"Guan Yu", "Zhang Fei", "bu", "Zhao Yun", "Ma Chao" }; Dialog=NewAlertdialog.builder ( This). Settitle ("Please select your initial warlord"). SetIcon (Android. R.drawable.ic_dialog_info)/** How to add a radio box: Setsinglechoiceitems (charsequence[] items, int * Checkeditem, onclicklist Ener listener) Items: Represents the individual text of the radio * Checkeditem: Indicates that the default selected Subscript listener is selected: Click Listener*/. Setsinglechoiceitems (Wujiang,0,NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {Toast.maketext (mainactivity). This,                                "The Warlord you choose is:" +Wujiang[which], toast.length_short). Show ();                    Dialog.dismiss ();    }}). Show (); }     Public voidBtn6_onclick (View view) {dialog=NewAlertdialog.builder ( This). Settitle ("Please choose the sport you like")                /** method to add a check box: Setmultichoiceitems (charsequence[] items, boolean[] * CheckedItems, Onmul                 Tichoiceclicklistener listener) * Items: The text that represents the check text; CheckedItems: The selected state of the check; Listener: Multi-menu-click Listener */. Setmultichoiceitems (NewString[] {"Basketball", "football", "swimming", "racing", "Running"},NULL,                        NULL). Setpositivebutton ("OK",NULL). Setnegativebutton ("Cancel",NULL). Show (); }     Public voidBtn7_onclick (View view) {dialog=NewAlertdialog.builder ( This). Settitle ("List of generals"). Setitems (NewString[] {"Lu Xianxian", "Zhao Zilong", "Evil to the Code Wei", "Cloud Long", "Zhang Yide" },                        NULL). Setpositivebutton ("OK",NULL). Setnegativebutton ("Cancel",NULL). Show (); }     Public voidBtn8_onclick (view view) {ImageView img=NewImageView ( This);        Img.setimageresource (R.drawable.goodby_times); Dialog=NewAlertdialog.builder ( This). Settitle ("Good-bye, time! "). Setview (IMG). Setpositivebutton ("OK",NULL). Show (); }     Public voidBtn9_onclick (view view) {//Instantiating layout layoutsLayoutinflater Inflater =Getlayoutinflater (); View Layout=inflater.inflate (R.layout.dialog1, (ViewGroup) Findviewbyid (r.id.dialog_layout)); Button Btn_login=(Button) Layout.findviewbyid (R.id.btn_login); FinalEditText uname =(EditText) Layout.findviewbyid (r.id.uname); FinalEditText pwd =(EditText) Layout.findviewbyid (R.ID.PWD); Btn_login.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {toast.maketext (mainactivity. This, "User name:" +uname.gettext () + "\ n Password:" +Pwd.gettext (), Toast.length_short). Show ();            Dialog.dismiss ();        }        }); Dialog=NewAlertdialog.builder ( This). Settitle ("User Login"). Setview (Layout). Show (); }    //The progress read box requires a simulated read    PrivateProgressDialog ProgressDialog =NULL; Private Final Static intmax_readprocess = 100;  Public voidBtn10_onclick (view view) {ProgressDialog=NewProgressDialog (mainactivity. This); Progressdialog.seticon (Android.        R.drawable.ic_dialog_info); Progressdialog.setprogress (0); Progressdialog.settitle ("Program Download ...");        Progressdialog.setprogressstyle (progressdialog.style_horizontal);        Progressdialog.setmax (max_readprocess);        Progressdialog.show (); NewThread (Progress). Start (); }    //new Open a thread, loop the accumulation, until 100 and then stop atRunnable progress =NewRunnable () {@Override Public voidrun () {intProgress = 0;  while(Progress <max_readprocess) {                Try{Thread.Sleep (50); Progressdialog.setprogress (++Progress); } Catch(interruptedexception e) {e.printstacktrace (); }            }             //after reading, the window disappears from theProgressdialog.cancel ();        }    };  Public voidBtn11_onclick (view view) {ProgressDialog=NewProgressDialog (mainactivity. This); Progressdialog.setmessage ("Trying to load data for you ..."); Progressdialog.setindeterminate (true); Progressdialog.setcancelable (true);     Progressdialog.show (); }}
Mainactivity.class

Source code:dialogshow.zip

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.