How to write the android message pop-up box

The construction methods of AlertDialog are all protected, so you can't create AlertDialog directly through the new AlertDialog.

The following examples are from the android Learning Manual, with 9 chapters in total, 108 examples. Just look at the source documents, and the examples are interactive and operable. The source code adopts the directory structure of android Studio, and the code is highlighted. Documents are displayed in the document structure diagram, which can be quickly located. Download from 360 mobile assistant, there is a shell on the icon.

To create an AlertDialog, you need to use the create () method in the AlertDialog. constructor

You need to know the following methods to create a dialog box using AlertDialog. Built by:

Set the title of the dialog box.

SetIcon: Set the icon for the dialog box.

SetMessage: sets the contents of the dialog box.

SetView: set a custom style for the dialog box.

SetItems: Sets the list to be displayed in the dialog box, which is usually used when several commands are displayed.

SetMultiChoiceItems: used to set the dialog box to display a series of check boxes.

SetNeutralButton: ordinary button

Set PositiveButton? : Add a "Yes" button to the dialog box

SetNegativeButton: Adds a "No" button to the dialog box.

Create: Create a dialog box

Display: Displays the dialog box.

First, a simple alarm dialog box.

Next, create a simple ALertDialog and display it:

Public class Dialog_AlertDialogDemoActivity extends Activity {?

/* * Called when an activity is first created. */?

@ Overwrite?

public void onCreate(Bundle savedInstanceState){?

super . oncreate(savedInstanceState); ?

setContentView(r . layout . main); ?

Dialog Alert Dialog = New Alert Dialog. The builder (this). ?

SetTitle ("Title of the dialog box").

SetMessage ("the contents of the dialog box").

setIcon(R.drawable.ic_launcher)。 ?

create(); ?

alert dialog . show(); ?

}?

}?

Bao com.tianjf

Import android.app.activity;

Import android.app.alertdialog;

Import android.app.dialog;

Import android.os.bundle;

Public class Dialog_AlertDialogDemoActivity extended Activity {

/* * Called when an activity is first created. */

@ Overlay

public void on create(Bundle saved instancestate){

super . oncreate(savedInstanceState);

setContentView(r . layout . main);

Dialog Alert Dialog = New Alert Dialog. The builder (this).

SetTitle ("Title of the dialog box").

SetMessage ("the contents of the dialog box").

setIcon(R.drawable.ic_launcher)。

create();

alert dialog . show();

}

} The running results are as follows:

Second, an alarm dialog box with buttons.

The above example is simple. Let's add a few buttons to this AlertDialog to realize the prompt dialog box of deletion operation.

[java] package com.tianjf?

Import android.app.activity; ?

Import android.app.alertdialog; ?

Import android.app.dialog; ?

Import android.content.dialoginterface; ?

Import android.os.bundle; ?

Public class Dialog_AlertDialogDemoActivity extends Activity {?

/* * Called when an activity is first created. */?

@ Overwrite?

public void onCreate(Bundle savedInstanceState){?

super . oncreate(savedInstanceState); ?

setContentView(r . layout . main); ?

Dialog Alert Dialog = New Alert Dialog. The builder (this). ?

SetTitle ("Are you sure you want to delete?" ).?

SetMessage ("Are you sure you want to delete this message?" ).?

setIcon(R.drawable.ic_launcher)。 ?

SetPositiveButton ("OK "), a new dialog interface. onclick listener () {?

@ Overwrite?

public void onClick(dialog interface dialog,int which) {?

// TODO automatically generated method stub?

}?

}).?

SetNegativeButton ("Cancel", a new dialog interface. onclick Listener () {?

@ Overwrite?

public void onClick(dialog interface dialog,int which) {?

// TODO automatically generated method stub?

}?

}).?

SetNeutralButton ("View Details", a new dialog interface. onclick listener () {?

@ Overwrite?

public void onClick(dialog interface dialog,int which) {?

// TODO automatically generated method stub?

}?

}).?

create(); ?

alert dialog . show(); ?

}?

}?

Bao com.tianjf

Import android.app.activity;

Import android.app.alertdialog;

Import android.app.dialog;

Import android.content.dialoginterface;

Import android.os.bundle;

Public class Dialog_AlertDialogDemoActivity extended Activity {

/* * Called when an activity is first created. */

@ Overlay

public void on create(Bundle saved instancestate){

super . oncreate(savedInstanceState);

setContentView(r . layout . main);

Dialog Alert Dialog = New Alert Dialog. The builder (this).

SetTitle ("Are you sure you want to delete?" ).

SetMessage ("Are you sure you want to delete this message?" ).

setIcon(R.drawable.ic_launcher)。

SetPositiveButton ("OK "), a new dialog interface. onclick Listener () {

@ Overlay

public void onClick(dialog interface dialog,int which) {

// TODO automatically generated method stub

}

}).

SetNegativeButton ("Cancel", a new dialog interface. onclick Listener () {

@ Overlay

public void onClick(dialog interface dialog,int which) {

// TODO automatically generated method stub

}

}).

SetNeutralButton ("View Details", a new dialog interface. onclick listener () {

@ Overlay

public void onClick(dialog interface dialog,int which) {

// TODO automatically generated method stub

}

}).

create();

alert dialog . show();

}

} In this example, we defined three buttons, namely "Yes" button, "No" button and a normal button. Each button has an onClick event, and you can do some processing with TODO after clicking the button.

Look at the running results:

You can see that three buttons have been added to AlertDialog, and three buttons have not added event handling. When clicked, they just close the dialog box and do nothing else.