带有一个,两个和三个按钮的Android Alert对话框

Sur*_*gch 21 android android-alertdialog

我不经常发出警报,但每次我这样做都需要一段时间来阅读文档并弄清楚如何去做.由于我现在必须这样做几次,我将在下面写下一个答案,我将来可以回来.具体来说,我想比较基本​​代码

  • 一键(OK)
  • 两个按钮(确定和取消)
  • 三个按钮(正面,负面,其他)

将这三种常见警报类型的基本代码放在一个位置以便将来进行简单参考和修改将会很不错.这个问题询问如何为一个按钮执行此操作.

我在下面添加我的答案.

Sur*_*gch 91

一键

在此输入图像描述

import android.support.v7.app.AlertDialog;

public class MainActivity extends AppCompatActivity {

    public void showAlertDialogButtonClicked(View view) {

        // setup the alert builder
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("My title");
        builder.setMessage("This is my message.");

        // add a button
        builder.setPositiveButton("OK", null);

        // create and show the alert dialog
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}
Run Code Online (Sandbox Code Playgroud)

两个按钮

在此输入图像描述

public class MainActivity extends AppCompatActivity {

    public void showAlertDialogButtonClicked(View view) {

        // setup the alert builder
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("AlertDialog");
        builder.setMessage("Would you like to continue learning how to use Android alerts?");

        // add the buttons
        builder.setPositiveButton("Continue", null);
        builder.setNegativeButton("Cancel", null);

        // create and show the alert dialog
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}
Run Code Online (Sandbox Code Playgroud)

三个按钮

在此输入图像描述

public class MainActivity extends AppCompatActivity {

    public void showAlertDialogButtonClicked(View view) {

        // setup the alert builder
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Notice");
        builder.setMessage("Launching this missile will destroy the entire universe. Is this what you intended to do?");

        // add the buttons
        builder.setPositiveButton("Launch missile", null);
        builder.setNeutralButton("Remind me later", null);
        builder.setNegativeButton("Cancel", null);

        // create and show the alert dialog
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果按钮文本太长而不能全部水平放置,那么它将自动布置在三个按钮的垂直列中.

在此输入图像描述

处理按钮单击

OnClickListenernull在上述实施例.null当用户点击按钮时,您可以用侦听器替换以执行某些操作.例如:

builder.setPositiveButton("Launch missile", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

        // do something like...
        launchMissile();
    }
});
Run Code Online (Sandbox Code Playgroud)

继续

您可以制作更多种类的对话框.请参阅文档以获取相关帮助.

由于a中仅支持三个按钮AlertDialog,因此这是带有列表的对话框的示例.

在此输入图像描述

public class MainActivity extends AppCompatActivity {

    public void showAlertDialogButtonClicked(View view) {

        // setup the alert builder
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Choose an animal");

        // add a list
        String[] animals = {"horse", "cow", "camel", "sheep", "goat"};
        builder.setItems(animals, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case 0: // horse
                    case 1: // cow
                    case 2: // camel
                    case 3: // sheep
                    case 4: // goat
                }
            }
        });

        // create and show the alert dialog
        AlertDialog dialog = builder.create();
        dialog.show();
    }
}
Run Code Online (Sandbox Code Playgroud)

有关单选按钮列表和复选框列表的类似示例,请参阅此答案.

笔记

  • 使用字符串资源而不是硬编码字符串.
  • 您可以将所有内容包装在一个扩展的类中,DialogFragment以便于重用对话框.(请参阅寻求帮助.)
  • 这些示例使用支持库来支持API 11之前的版本.因此导入应该是

    import android.support.v7.app.AlertDialog;
    
    Run Code Online (Sandbox Code Playgroud)
  • onCreate为简洁起见,我省略了上述示例中的方法.那里没什么特别的.

也可以看看