Android dialog Box Dialog

Source: Internet
Author: User
<span id="Label3"></p><span style="font-size:18px">dialog box Dialog<br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>What is a dialog box<br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>A dialog box is a small window that pops up on top of the current page to display important hints, prompts the user for input, confirms the message,<br><span style="white-space:pre"><span style="white-space:pre"></span></span><br><span style="white-space:pre"><span style="white-space:pre"></span></span>or display a status. for example, to display a progress bar dialog, exit the Prompt.<br><br>Features of the dialog box:<br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>1, the current interface pop-up small window.<br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>2, the user wants to interact with it, can receive the user input information, also may feedback the information to the User.<br><br>Common Dialog Boxes:<br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>1, Normal dialog box<span style="white-space:pre"><span style="white-space:pre"> </span></span>Alertdialog<span style="white-space:pre"><span style="white-space:pre"></span></span><br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>2, Progress Bar dialog Box ProgressDialog<br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>3, Date dialog Box Datepickerdialog<br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>4, Time dialog Box Timepickerdialog<br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>5, Customize dialog box<br><br>Common common dialog box Alertdialog<span style="white-space:pre"><span style="white-space:pre"></span></span><br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>1, General List dialog box Setitems--onclicklistener<br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>2, single-selection dialog box<span style="white-space:pre"><span style="white-space:pre"> </span></span>Setsinglechoiceitems--onclicklistener<br><br><span style="white-space:pre"><span style="white-space:pre"></span></span>3, multi-select Dialog box<span style="white-space:pre"><span style="white-space:pre"> </span></span>Setmultichoiceitems---> Onmultichoiceclicklistener<br><br></span><p><p><span style="font-size:18px"><span style="white-space:pre"></span>4, Adapter dialog Box Setadapter--onclicklistener</span></p></p><p><p><span style="font-size:18px"></span></p></p><pre code_snippet_id="1867485" snippet_file_name="blog_20160905_1_6674657" name="code" class="java">Package Com.qf.day11_dialog_01;import Java.util.calendar;import Android.app.activity;import Android.app.alertdialog;import Android.app.datepickerdialog;import Android.app.datepickerdialog.ondatesetlistener;import Android.app.progressdialog;import Android.app.timepickerdialog;import Android.app.timepickerdialog.ontimesetlistener;import Android.content.dialoginterface;import Android.content.dialoginterface.onclicklistener;import android.os.Bundle; Import Android.view.view;import Android.widget.datepicker;import Android.widget.edittext;import Android.widget.timepicker;import Android.widget.toast;public class Mainactivity extends Activity {private EditText Nam E;private EditText pwd; @Override protected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstan cestate); Setcontentview (r.layout.activity_main); } public void Showalertdialog (View V) {//1, which defines the builder of the normal dialog box Alertdialog.builder builder = new Alertdialog.builder (mainactivity.this); //2, Build dialog box Builder.seticon (r.drawable.img_23),//set icon Builder.settitle ("hint"),//set caption Builder.setmessage ("confirm exit?"); Set Content//set Cancel button/** * Text button to display the type * listener button corresponding to the Listener */builder.setnegativebutton ("cancel", nu ll); Set OK button Builder.setpositivebutton ("ok", new Onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, i NT Which) {finish ();}}); 3, Display dialog box Builder.show (); } public void Showprogressdialog (View v) {//1, gets ProgressDialog object ProgressDialog pdialog = new Progressdialo G (this); 2, Build dialog box Pdialog.seticon (r.drawable.img_14); Pdialog.settitle ("hint"); Pdialog.setmessage ("loading, Please later ...."); Pdialog.setcancelable (true);//can be undone true can be false cannot//sets the style of the progress bar dialog Box//progressdialog.style_horizontal Horizontal Pdialog.setprogressstyle (progressdialog.style_horizontal) Progressdialog.style_spinner in circles; 3, Display dialog box Pdialog.show (); Pdialog.dismiss ();//dialog Box disappears Pdialog.setprogress (50);//set The Progress value after the dialog is displayed before you can set} public void Showdatepickerdialog (View V) {//get The current date of the system Calendar Calendar = Calendar.getinstance (); int year = Calendar.get (calendar.year); int monthofyear = Calendar.get (calendar.month); int dayofmonth = Calendar.get (calendar.day_of_month); 1. Create a dialog box with a date selector/** * Context Context Object * Callbackondatesetlistener ("ok" button listener on date Selector) * Year default years * Montho Fyear Default Month * dayofmonth default date */datepickerdialog Dpdialog = new Datepickerdialog (this,new Ondatesetlisten ER () {/** * DatePicker View date selector */@Overridepublic void ondateset (DatePicker view, int year, int Monthofyear , int Dayofmonth) {//select the date after monitoring Toast.maketext (mainactivity.this, year+ "-" + (monthofyear+1) + "-" +dayofmonth, Toast.length_short). show ();}, year, monthofyear, dayofmonth); 2, Display dialog box Dpdialog.show (); } public void Showtimepickerdialog (View v) {//1, Creating a dialog box with a time selector/** * context Context Object * CallBack Ontimesetlistener * hourofday Default hour * Minute default number of minutes * Is24hourview whether 24 hours * * Timepickerdialog tpdialog = new Timepickerdialog (this, new Ontimesetlistener () {@Overridepublic void Ontimeset (timepick er view, int hourofday, int Minute) {toast.maketext (mainactivity.this, hourofday + ":" + minute, toast.length_short). Show ();}}, n, n, true); 2, Display dialog box Tpdialog.show (); }//custom dialog box (implemented on a normal dialog) public void Showcustomdialog (View v) {//1, Gets the builder of the normal dialog box Alertdialog.builder Builder = new Alertdialog.builder (this); 2, Build dialog box Builder.seticon (r.drawable.img_23); Builder.settitle ("user login"); 3, set the custom view for the dialog box. Getlayoutinflater (). inflate (r.layout.login, null); Name = (EditText) View.findviewbyid (r.id.name); PWD = (EditText) View.findviewbyid (r.id.pwd); Builder.setview (view); Add button Builder.setnegativebutton ("cancel", null) for the dialog box; Builder.setpositivebutton ("login", new Onclicklistener () {@Overridepublic void onClick (dialoginterface dialog, int which) {String info = name.gettext (). ToString () + "," + pwd.gettext (). ToString (); Toast.maketext (mainactivity.this, info, toast.length_short). show ();}}); 4, Display dialog box Builder.show (); }}</pre><br><br><p><p></p></p><p><p><span style="font-size:18px"></span></p></p><pre code_snippet_id="1867485" snippet_file_name="blog_20160905_2_327965" name="code" class="java">Package Com.qf.day11_dialog_02;import Android.app.activity;import Android.app.alertdialog;import Android.content.dialoginterface;import Android.content.intent;import Android.content.dialoginterface.onclicklistener;import Android.content.dialoginterface.onmultichoiceclicklistener;import Android.graphics.color;import Android.os.bundle;import Android.view.view;import Android.widget.arrayadapter;import android.widget.TextView; public class Mainactivity extends Activity {private TextView tv;private int position;//single-selection List of intermediate transfers @Override PROTECTE d void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); TV = (TextView) Findviewbyid (r.id.tv); }//normal List dialog public void Showcommon (View v) {//1, Gets the builder of the normal dialog box Alertdialog.builder builder = new Alertdialog . Builder (this); Builder.seticon (r.drawable.ic_launcher); Builder.settitle ("select color"); 2, Set list//getresources (). GetstrinGarray (id) builder.setitems (r.array.colors, new Onclicklistener () {/** * Dialoginterface Dialog dialog box * int which the currently selected item starts at 0 */@Overridepublic void onClick (dialoginterface dialog, int which) {switch (which) {case 0:tv.sett Extcolor (color.red); break;case 1:tv.settextcolor (color.green); break;case 2:tv.settextcolor (Color.BLUE); break;}}); 3, Display dialog box Builder.show (); }//radio list public void Showsingle (View V) {//1, the builder of the normal dialog box Alertdialog.builder builder = new Alertdialo G.builder (this); Builder.seticon (r.drawable.ic_launcher); Builder.settitle ("set Text size"); 2, Set the Radio list/** * The ID of the Items option resource r.array.sizes * Checkeditem the subscript of the default selected item, if not checked by default, set to 1 * Listener listener */builder.setsinglechoiceitems (r.array.sizes,-1, new Onclicklistener () {@Overridepublic void OnClick (dialoginter Face dialog, int Which) {//assigns The subscript of the currently selected item to the global variable positionposition = which;}}); Builder.setpositivebutton ("ok", New OnClickListener () {@Overridepublic void onClick (dialoginterface dialog, int Which) {//TODO determines what action to perform based on global position switch (pos Ition) {case 0:tv.settextsize; break;case 1:tv.settextsize (+); break;case 2:tv.settextsize (+); break;}}); 3, Display dialog box Builder.show (); }//multiple-selection list public void Showmulti (View V) {//1, the builder of the normal dialog box Alertdialog.builder builder = new Alertdialog.bui Lder (this); Builder.seticon (r.drawable.ic_launcher); Builder.settitle ("set content"); 2. Set multi-select list/** * Items Resource ID * CheckedItems currently selected item * Listener Listener *///get All data final string[] H Obbys = getresources (). getstringarray (r.array.hobbys); Selected item Final boolean[] checkeditems = new boolean[hobbys.length]; Builder.setmultichoiceitems (r.array.hobbys, checkeditems, New Onmultichoiceclicklistener () {/** * DialogInterface Dialog Current Dialog * int which the currently selected item subscript * Boolean ischecked currently selects the status of item */@Overridepublic void OnClick (dialoginterfa Ce dialog, int which, boolean isChecked) {//change boolean[] state checkeditems[which] = isChecked;}}); Builder.setpositivebutton ("ok", new Onclicklistener () {@Overridepublic void onClick (dialoginterface dialog, int Which) {StringBuilder sbuilder = new StringBuilder (); for (int i=0;i<checkeditems.length;i++) {if (checkeditems[i]) { Sbuilder.append (hobbys[i]). Append ("");}} Tv.settext (sbuilder.tostring ());}); 3, Display dialog box Builder.show (); }//adapter list public void Showadapter (View V) {//1, the builder of the normal dialog box Alertdialog.builder builder = new Alertdialog. Builder (this); Builder.seticon (r.drawable.ic_launcher); Builder.settitle ("select settings"); 2, set adapter string[] data = getresources (). Getstringarray (r.array.settings); arrayadapter<string> adapter = new Arrayadapter<string> (this, android. r.layout.simple_list_item_1, data); Builder.setadapter (adapter, new Onclicklistener () {@Overridepublic void onClick (dialoginterface dialog, int WHich) {Intent Intent = new Intent (); switch (which) {case 0:intent.setaction ("android.settings.WIFI_SETTINGS"); Case 1:intent.setaction ("android.settings.SOUND_SETTINGS"); break;case 2:intent.setaction (" Android.settings.SETTINGS "); break;} StartActivity (intent);}}); 3, Display Dialog Box//builder.show (); Builder.create (). Show (); }}</pre><br><br><p><p></p></p><p><p>Android dialog Box Dialog</p></p></span>

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.