In Android development, we often need to pop up some dialogs on the android interface, such as asking users or asking users to choose. These functions are called the android dialog box. During the process of using Android, I summarized seven types of Android dialog, the following describes how to use these seven Android dialog dialogs.
1. When you press the return button, a prompt is displayed to ensure correct operations. A Common Dialog Box style is used.
The code for creating the dialog box is as follows:
Protected void Dialog () {alertdialog. Builder = new Builder (main. This); builder. setmessage ("are you sure you want to exit? "); Builder. settitle ("prompt"); builder. setpositivebutton ("OK", 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 ();}
Call this method 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 in the dialog box and added three buttons.
The code for creating a dialog is as follows:
Dialog dialog = new alertdialog. Builder (this). seticon (Android. R. drawable. btn_star). settitle ("Preferences"). setmessage ("do you like Jet Li's movies? "). Setpositivebutton ("very fond", new onclicklistener () {@ override public void onclick (dialoginterface dialog, int 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, int 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, "I do not like it or not. ", Toast. length_long). Show () ;}}). Create (); dialog. Show ();
3. The information content is a simple view type
The code for creating the dialog method is as follows:
New alertdialog. builder (this ). settitle ("enter "). seticon (Android. r. drawable. ic_dialog_info ). setview (New edittext (this )). setpositivebutton ("OK", null ). setnegativebutton ("cancel", null ). show ();
4. The information content is a group of single queues
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. The information content is a set of multiple selection boxes
The code for creating the dialog method is as follows:
New alertdialog. builder (this ). settitle ("single answer "). seticon (Android. r. drawable. ic_dialog_info ). setsinglechoiceitems (New String [] {"Item1", "item2"}, 0, new dialoginterface. onclicklistener () {public void onclick (dialoginterface diich, int which) {dialog. dismiss ();}}). setnegativebutton ("cancel", null ). show ();
6. Information Content is a set of simple list items
The code for creating a dialog is as follows:
New alertdialog. builder (this ). settitle ("list box "). setitems (New String [] {"Item1", "item2"}, null ). setnegativebutton ("OK", null ). show ();
7. Information Content is a custom Layout
The code of the dialog layout file 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 ();
Now, the usage of the above seven Android dialog boxes is introduced here, basically all. If you encounter dialog during Android development, you can take a look at it.
From: http://www.oschina.net/question/54100_32486