Android dialog use

Translated from: Development->API Guide->User Interface & Navigation->Dialogs

Notice:

dialog is a base class, but we should avoid using dialog directly as much as possible, but should use its subclasses, such as AlertDialog, DatePickerDialog or TimePickerDialog, etc.

Use DialogFragment to manage dialogs, which will allow your app to properly control the dialog lifecycle when the user hits the back button and rotates the screen. DialogFragment is basically the same as the traditional Fragment.

Creating a Dialog Fragment

Create a basic AlertDialog using DialogFragment

public class FireMissilesDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.dialog_fire_missiles)
               .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}

If you create the above dialog and call the show() method, you will see the dialog as shown below.

image

Building an Alert Dialog

Alert Dialog consists of three parts, title, content and button, as shown in the following figure

image

Create an Alert Dialog

// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

// 2. Chain together various setter methods to set the dialog characteristics
builder.setMessage(R.string.dialog_message)
       .setTitle(R.string.dialog_title);

// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();

add button

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Add the buttons
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // User clicked OK button
           }
       });
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               // User cancelled the dialog
           }
       });
// Set other dialog properties
...

// Create the AlertDialog
AlertDialog dialog = builder.create();

You can also add a list to the dialog, and you can check the official documentation for details.

Creating a Custom Layout

If you want a custom layout in a dialog, create a layout and add it to an AlertDialog by calling setView() on your AlertDialog.Builder object.

If you want to use a custom layout, you can call the setView() method in AlertDialog.Builder to add the custom layout to the AlertDialog.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.dialog_signin, null))
    // Add action buttons
           .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // sign in the user ...
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   LoginDialogFragment.this.getDialog().cancel();
               }
           });
    return builder.create();
}

Tip: If you want to create a custom dialog, you can display an Activity instead of the dialog, just

<activity android:theme="@android:style/Theme.Holo.Dialog" >

Done, so the activity will be displayed as a dialog instead of full screen.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325248381&siteId=291194637