Android dialog box (DIALOG) create your own dialog box)

Source: Internet
Author: User
Create your own dialog box in the android dialog box (DIALOG)

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 is the owner of each dialog box, which automatically manages the status of each dialog box and attaches it to activity. In this way, each dialog box inherits the specific owner 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
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), you should initialize the dialog box dialog here. This callback method is passed to showdialog (int
The same 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 every time a dialog box is opened, while oncreatedialog (INT)
It 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 in the dialog box
ID, and the dialog box object created in oncreatedialog.

 

Dismissdialog (INT ):When
When you are about to close the dialog box, you can call dismiss () to delete it. You can also call
The dismissdialog (int id) method, which will call the dismiss () method for this dialog box.
If you want to use oncreatedialog (int id)
Method to manage the status of your dialog box (as discussed in the previous chapter). Each time your dialog box is cleared, the status of this dialog box object will be retained by this activity. If you decide
It is important that you no longer need this object or clear the state, so 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 answer "). 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 ();

Original link http://www.cnblogs.com/salam/archive/2010/11/15/1877512.html

Learning materials:

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

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

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.