Android dialog box-Part 1-create your own dialog box

Source: Internet
Author: User

Activities provides a dialog box mechanism for convenient management, suchonCreateDialog(int),onPrepareDialog(int, Dialog),showDialog(int),Dismissdialog (INT) and other methods. If you use these methods, the activity willThe getowneractivity () method returns the dialog box (DIALOG) managed by the activity ).

 

  Oncreatedialog (INT): When you use this callback function, the android system will effectively set this activity as the owner of each dialog box, so as to automatically manage the status of each dialog box and link it to the activity. In this way, each dialog box inherits the specific attributes of this activity. For example, when a dialog box is opened, the menu key displays the option menu defined for the activity, and the volume key modifies the audio stream used by the activity.

 

  Showdialog (INT): When you want to display a dialog box, call the showdialog (int id) method and pass an integer that uniquely identifies the dialog box. When the dialog box is requested for the first time, Android calls oncreatedialog (int id) from your activity. You should initialize the dialog box dialog here. This callback method is passed to the same ID as showdialog (int id. After you create this dialog box, return this object at the end of the activity.

 

  Onpreparedialog (INT, DIALOG): Before the dialog box is displayed, Android also calls the optional callback function onpreparedialog (int id, DIALOG ). if you want to change any of its attributes when each dialog box is opened, you can define this method. This method is called each time a dialog box is opened, while oncreatedialog (INT) is called only when the dialog box is opened for the first time. If you do not define onpreparedialog (), this dialog box will remain the same as the previous one. This method is also passed with the dialog box ID and the dialog box object created in oncreatedialog.

 

  dismissDialog(int):When you want to close the dialog box, you can call dismiss () to delete it. If needed, you can also call the dismissdialog (int id) method from this activity, which will actually call the dismiss () method for this dialog box. If you want to use the oncreatedialog (int id) method to manage the status of your dialog box (as discussed in the previous chapter), and each time your dialog box is cleared, the status of the dialog box objects will be retained by the activity. If you decide that you no longer need this object or clear the state, you should call removedialog (int id ). This will delete any internal object reference. If this dialog box is displayed, it will be removed.

 

The following figure shows the effects of several dialogs.

Figure 1

Figure 2

Figure 3

Figure 4

Figure 5

Figure 6

Figure 7

  Figure 1 effect:When you press the return button, a prompt is displayed to ensure that the operation is correct. A Common Dialog Box style is used.

Code:

Create dialog box method Dialog ()

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;
}

  Figure 2 effect:Changed the chart in the dialog box and added three buttons.

Dialog dialog = new alertdialog. Builder (this). seticon (
Android. R. drawable. btn_star). settitle ("Preferences"). setmessage (
"Do you like Jet Li's film? "). 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, "Do not like it or not. ", Toast. length_long)
. Show ();
}
}). Create ();

Dialog. Show ();

 

Figure 3 effect:Information Content is a simple view type

New alertdialog. Builder (this). settitle ("enter"). seticon (
Android. R. drawable. ic_dialog_info). setview (
New edittext (this). setpositivebutton ("OK", null)
. Setnegativebutton ("cancel", null). Show ();

 

Figure 4 effect:The information content is a group of single queues

New alertdialog. Builder (this). settitle ("check box"). setmultichoiceitems (
New String [] {"Item1", "item2"}, null, null)
. Setpositivebutton ("OK", null)
. Setnegativebutton ("cancel", null). Show ();

 

Figure 5 effect:The information content is a set of multiple selection boxes

New alertdialog. Builder (this). settitle ("single button"). 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 ();

 

Figure 6 effect:Information Content is a set of simple list items

New alertdialog. Builder (this). settitle ("list box"). setitems (
New String [] {"Item1", "item2"}, null). setnegativebutton (
"OK", null). Show ();

 

Figure 7 effect:Information Content is a custom Layout

1. layout File

<? 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>

2. Call Code

Layoutinflater Inflater = getlayoutinflater ();
View layout = Inflater. Inflate (R. layout. diater,
(Viewgroup) findviewbyid (R. Id. Dialog ));

New alertdialog. Builder (this). settitle ("custom layout"). setview (layout)
. Setpositivebutton ("OK", null)
. Setnegativebutton ("cancel", null). Show ();

 

References:

Http://android.blog.51cto.com/268543/333769

Http://android.yaohuiji.com/archives/655

Http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html

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.