[Reprinted] Create your own Dialog box in the Android Dialog box (Dialog)

Source: Internet
Author: User

 

Activities provides a convenient Dialog box mechanism for management, such as onCreateDialog (int), onPrepareDialog (int, Dialog), showDialog (int), and dismissDialog (int) if you use these methods, the Activity returns the dialog box managed by the Activity through the getOwnerActivity () method ).

 

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 are about 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 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" androidrientation = "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 ();

 

Learning materials:

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

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

 

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.