带有一个,两个和三个按钮的Android Alert对话框 [英] Android Alert Dialog with one, two, and three buttons

查看:168
本文介绍了带有一个,两个和三个按钮的Android Alert对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I don't make alerts very often but every time I do it takes me a while to read through the documentation and figure out how to do it. Since I have had to do this a few times now, I am going to write an answer below that I can come back to in the future. Specifically I want to compare the basic code for

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

最好将这三种常见警报类型的基本代码放在一个位置,以便将来方便参考和修改. 此问题询问如何对一个按钮进行操作.

It would be nice to have the basic code for these three common alert types in one spot for easy reference and modification in the future. This question asks how to do it for one button.

推荐答案

一个按钮

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.

在以上示例中,OnClickListenernull.您可以将null替换为侦听器,以在用户点击按钮时执行某些操作.例如:

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

继续

您可以创建更多种类的对话框.请参阅文档以获得帮助.

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

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.

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

  • Use string resources rather than hard coded strings.
  • You can wrap everything in a class that extends DialogFragment for easy reuse of a dialog. (See this for help.)
  • These examples used the support library to support versions prior to API 11. So the import should be

import android.support.v7.app.AlertDialog;

  • 为简洁起见,我在上面的示例中省略了onCreate方法.那里没有什么特别的.

  • I omitted the onCreate method in the examples above for brevity. There was nothing special there.

    • How to disable the positive button
    • Use a Toast rather than an Alert for short messages
    • Single-choice list, radio button list, and checkbox list
    • How to implement a custom AlertDialog View

    这篇关于带有一个,两个和三个按钮的Android Alert对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆