【问题标题】:Android Alert Dialog with one, two, and three buttons带有一个、两个和三个按钮的 Android 警报对话框
【发布时间】:2017-09-16 18:09:55
【问题描述】:

我不经常发出警报,但每次我都需要一段时间来阅读documentation 并弄清楚如何做到这一点。由于我现在不得不这样做几次,我将在下面写一个答案,我可以在未来回来。具体我想比较一下

的基本代码
  • 一键(确定)
  • 两个按钮(确定和取消)
  • 三个按钮(正、负、其他)

如果将这三种常见警报类型的基本代码放在一个位置,以方便日后参考和修改,那就太好了。 This question 询问如何操作一键。

【问题讨论】:

    标签: android android-alertdialog


    【解决方案1】:

    一个按钮

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

    如果按钮文本太长而无法水平放置,那么它将自动排列在三个按钮的垂直列中。

    处理按钮点击

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

    builder.setPositiveButton("Launch missile", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
    
            // do something like...
            launchMissile();
        }
    });
    

    继续

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

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

    有关单选按钮列表和复选框列表的类似示例,请参阅this answer

    注意事项

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

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

    另见

    【讨论】:

    • 我需要将哪个视图传递给对话框?
    • @Eduardo Lion,我假设您指的是showAlertDialogButtonClicked(View view) 中的view。这只是按钮的onClick() 方法中的按钮。你可以忽略它。该对话框根本不使用它。对话框只需要Context,在本例中为this,即Activity。
    • 我对肖像和您的三个按钮示例有疑问。按钮之间没有太多空间。如何修改布局?主要问题是默认布局的按钮文本长度似乎很长。
    • @testing,抱歉,在切换到 Flutter 之后,我已经有几年没有研究过 Android 警报对话框了,所以如果我知道你问题的答案,我现在已经忘记了。
    • @Suragch:感谢您的回复。最后,我在我的android:alertDialogTheme 中为我的android:buttonBarButtonStyle 添加了一个<item name="android:padding">10dp</item>。似乎工作。
    猜你喜欢
    • 2012-01-03
    • 2011-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多