7 Types of Android dialog usages _android

Source: Internet
Author: User
Tags stub

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 the Android Dialog dialog, in the process of using Android, I summed up, the android dialog type is no more than 7 kinds, I would like to introduce the 7 Kinds of Android Dialog dialog box to use the method, I hope we can be of some help.
1. The effect is to press the return button when a pop-up prompts to ensure that the correct operation, using the common style of the dialog box.

Create the Dialog dialog box method code as follows:

protected void Dialog () {
Alertdialog.builder Builder = new Builder (main.this);
Builder.setmessage ("Confirm exit"?)   ");
Builder.settitle ("hint");
Builder.setpositivebutton ("Confirm", new Onclicklistener () {  @Override public  void OnClick ( Dialoginterface dialog, int which) {  Dialog.dismiss ();  Main.this.finish ();  }
});
Builder.setnegativebutton ("Cancel", new Onclicklistener () {  @Override public  void OnClick (   Dialoginterface dialog, int which) {  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.getre   Peatcount () = = 0) {  dialog ();
return
false;
}  

2. Changed the dialog box chart, added three buttons

The method code to create the dialog is as follows:

 Dialog Dialog = new Alertdialog.builder (this). SetIcon (Android. R.drawable.btn_star). Settitle ("Hobby Survey"). Setmessage ("Do you like Jet Li's movies?"). "). Setpositivebutton (" Very like ", New Onclicklistener () {@Override public void OnClick (Dialoginterface dialog, I NT which) {//TODO auto-generated Method Stub toast.maketext (Main.this, "I like his movies very much."
     ", Toast.length_long). Show (); }). Setnegativebutton ("Dislike", new Onclicklistener () {@Override public void OnClick (Dialoginterface dialog, I NT which) {//TODO auto-generated Method Stub toast.maketext (Main.this, "I don't like his movies."
    ", Toast.length_long). Show (); }). Setneutralbutton ("General", new Onclicklistener () {@Override public void OnClick (dialoginterface dialog, int which) {//TODO auto-generated Method Stub toast.maketext (Main.this, "not to be liked."
    ", Toast.length_long). Show ();
    ). Create ();
Dialog.show (); 

3. Information content is a simple view type

The Code for creating the dialog method is as follows:

New Alertdialog.builder (this). Settitle ("Please enter"). SetIcon (   android. R.drawable.ic_dialog_info). Setview (   new EditText (This)). Setpositivebutton ("OK", null).   Setnegativebutton ("Cancel", null). Show ();      

4. Information content is a group of radio boxes

The Code for creating the dialog method is as follows:

New Alertdialog.builder (this). Settitle ("check box"). Setmultichoiceitems (   new string[] {"Item1", "Item2"}, NULL , null)   . Setpositivebutton (OK, null)   . Setnegativebutton ("Cancel", null). Show ();      

5. Information content is a set of multiple selection boxes

The Code for creating 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   void OnClick (dialoginterface dialog, int which) {    Dialog.dismiss ();   }   }). Setnegativebutton ("Cancel", 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 ("ListBox"). Setitems (   new string[] {"Item1", "Item2"}, NULL). Setnegativebutton (   OK, null). Show ();    

7. Information content is a custom layout

The dialog layout file code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns: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" >
<textview android:layout_height= "Wrap_content"  android:layout_width= "wrap_content"
android:id= "@+id/tvname" android:text= "Name:"/>
<edittext android:layout_height= "wrap_content" android:layout_width= "WRAP_"
Content "android:id=" @+id/etname "android:minwidth=" 100dip "/>
</LinearLayout>  

The Code for creating the dialog method is as follows:

Layoutinflater inflater = Getlayoutinflater ();  View layout = Inflater.inflate (R.layout.dialog,   (viewgroup) Findviewbyid (R.id.dialog));  New Alertdialog.builder (this). Settitle ("Custom Layout"). Setview (Layout).   Setpositivebutton (OK, null)   . Setnegativebutton ("Cancel", null). Show ();          

OK, the above 7 Kinds of Android Dialog dialog box to use the method is introduced here, basically all, if you are in the Android Development process encountered dialog when you can take out to see.

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.