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

苏拉奇:

我不会经常发出警报,但是每次我都会花一些时间阅读文档并弄清楚如何做。由于我现在必须做几次,因此我将在下面写下一个答案,以后我会再说回来。具体来说,我想比较基本​​代码

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

最好将这三种常见警报类型的基本代码放在一个位置,以便将来方便参考和修改。这个问题问一个按钮怎么做。

苏拉奇:

一键式

在此处输入图片说明

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();
    }
}

两个按钮

在此处输入图片说明

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();
    }
}

三个按钮

在此处输入图片说明

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();
    }
}

If the button text it too long to all fit horizontally, then it will automatically get laid out in a vertical column of three buttons.

在此处输入图片说明

Handling Button Clicks

The OnClickListener was null in the above examples. You can replace null with a listener to do something when the user taps a button. For example:

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

        // do something like...
        launchMissile();
    }
});

Going On

There are many more varieties of dialogs that you can make. See the documentation for help with this.

Since only three buttons are supported in an AlertDialog, here is an example of a dialog with a list.

在此处输入图片说明

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();
    }
}

See this answer for similar examples of a radio button list and a checkbox list.

Notes

  • Use string resources rather than hard coded strings.
  • 您可以将所有内容包装在扩展的类中,DialogFragment以方便对话框的重用。(请参阅以获得帮助。)
  • 这些示例使用支持库来支持API 11之前的版本。因此,导入应为

    import android.support.v7.app.AlertDialog;
    
  • onCreate为了简洁起见,在上面的示例中省略了该方法。那里没有什么特别的。

也可以看看

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

关闭和重置一个模式对话框

对话框仅显示一个按钮

如何在JavaFX 8 Alert对话框中更改是/否按钮的文本

在Android Alert对话框中使用HTML

如何在“对话框”的顶部布置面板,使其具有两个按钮?

两个对话框setAlwaysOnTop(true):生成的第二个对话框在第一个对话框之后

如何在Android Alert对话框中显示列表视图?

如何在Android Alert对话框中添加第三个按钮?

没有对话框的Android对话框setNegativeButton

在对话框Android之后创建另一个对话框

两个对话框中的循环依赖

Android Alert对话框自定义视图与父高不匹配

如何用jQuery UI对话框替换window.alert('')-合并两个脚本

如何使用RFlutter Alert在启动屏幕上显示对话框,或者仅弹出对话框并在没有wifi时退出

在Linux(基本OS)上的VALA中,带有图标,文本和两个按钮的简单对话框

Sweet Alert对话框的确认按钮显示得太细

单击Android屏幕的Alert对话框的Keyevent

使用第一个对话框的关闭图标关闭两个对话框窗口

在模式对话框中添加三个提交按钮?

Android Alert对话框不会关闭

无法从Alert对话框中的RatingBar获取值和异常错误(Android)

Android Alert对话框-如果语句不起作用

返回上一个活动,在android中有一个对话框

当我在Android活动中按下一个按钮时,两个警报对话框均显示

Android Close Alert对话框和Activity一起

带有listview的导航抽屉突出显示对话框案例的两个列表项

wxPython-显示打印在第三个对话框上的两个对话框中的文本

两个活动的一个自定义警报对话框

双击BottomSheetDialog快速给出两个对话框