7 Types of android dialog use examples

Source: Internet
Author: User

In Android development, we often need to pop up some dialog boxes on the Android interface, such as asking the user or letting the user choose. These features we call it Android Dialog dialog, in the course of our use of Android, I summed up, the type of android dialog is nothing more than 7, I would like to introduce you to the following 7 Kinds of Android Dialog dialog box use method, I hope we can help you.

1. This effect is a prompt that pops up when you press the Back button to ensure error-correct operation, using a common dialog box style.

The method code for creating the Dialog dialog box is as follows:

protected voiddialog () {Alertdialog.builder Builder=NewBuilder (Main. This); Builder.setmessage ("Are you sure you want to quit? "); Builder.settitle (Prompted); Builder.setpositivebutton ("Confirm",NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich)      {Dialog.dismiss (); Main. This. Finish ();    }   }); Builder.setnegativebutton ("Cancel",NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich)     {Dialog.dismiss ();    }   });   Builder.create (). Show (); }
This method is called in the onkeydown (int keycode, KeyEvent event) method
 Public boolean onKeyDown (int  keycode, KeyEvent event) {   if (keycode = =  Keyevent.keycode_back && event.getrepeatcount () = = 0) {   dialog ();  }   return false   ; }
2. Changed the chart of the dialog box, added three buttons

The method code to create the dialog is as follows:

Dialog Dialog =NewAlertdialog.builder ( This). SetIcon (Android. R.drawable.btn_star). Settitle ("Preferences Survey"). Setmessage ("Do you like Jet Li's movies?" "). Setpositivebutton (" very like ",NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {//TODO auto-generated Method StubToast.maketext (Main. This, "I like his movie very much. ", Toast.length_long). Show (); }}). Setnegativebutton ("Don't like",NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {//TODO auto-generated Method StubToast.maketext (Main. This, "I don't like his films. ", Toast.length_long). Show (); }}). Setneutralbutton ("General",NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {//TODO auto-generated Method StubToast.maketext (Main. This, "I don't like it. ", Toast.length_long). Show ();     }}). Create (); Dialog.show ();
3. Information content is a simple view type

The code to create the dialog method is as follows:

New Alertdialog.builder (this). Settitle ("Please enter"). SetIcon (     android. R.drawable.ic_dialog_info). Setview (      new EditText (thisnull)     . Setnegativebutton (null). Show ();
4. Information content is a set of radio boxes

The code to create the dialog method is as follows:

New Alertdialog.builder (this). Settitle ("check box"). Setmultichoiceitems (      newnull NULL )     . Setpositivebutton (null).     Setnegativebutton (null). Show ();
5. Information content is a set of multi-marquee

The code to create the dialog method is as follows:

New Alertdialog.builder (this). Settitle ("Radio Box"). SetIcon (     android. R.drawable.ic_dialog_info). Setsinglechoiceitems (      new string[] {"Item1", "Item2"}, 0,     
    New  Dialoginterface.onclicklistener () {      public voidint which)  {       Dialog.dismiss ();      }     }). Setnegativebutton (null). Show ();
6. Information content is a set of simple list items

The method code to create the dialog is as follows:

New Alertdialog.builder (this). Settitle ("list box"). Setitems (      newnull). Setnegativebutton (      null). Show ();
7. Information content is a custom layout

Dialog layout file code is as follows:

<?XML version= "1.0" encoding= "Utf-8"?>     <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_height= "Wrap_content"Android:layout_width= "Wrap_content"Android:background= "#ffffffff"android:orientation= "Horizontal"Android:id= "@+id/dialog">     <TextViewAndroid:layout_height= "Wrap_content"Android:layout_width= "Wrap_content"Android:id= "@+id/tvname"Android:text= "Name:" />     <EditTextAndroid:layout_height= "Wrap_content"Android:layout_width= "Wrap_content"Android:id= "@+id/etname"Android:minwidth= "100dip"/>    </LinearLayout>
The code to create the dialog method is as follows:
Layoutinflater inflater = getlayoutinflater ();      = inflater.inflate (r.layout.dialog,     (viewgroup) Findviewbyid (R.id.dialog));    New Alertdialog.builder (this). Settitle ("Custom Layout"). Setview (Layout)     . Setpositivebutton (null  )     . Setnegativebutton (null). Show ();

OK, the above 7 Kinds of Android Dialog dialog box use method introduced here, basically all, if everyone in the Android development process encountered dialog time can take out to see.

Also note that this article refers to the article:
Http://android.tgbus.com/Android/tutorial/201107/359812.shtml

7 Types of android dialog use examples

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.