【问题标题】:Creating UIAlert创建 UIAlert
【发布时间】:2018-08-12 10:38:00
【问题描述】:

我是一个 android 工作室的新手,以前在 swift 中编码。我试图研究在 android studio 中创建一个警报视图,但其中大多数没有给我超过 2 个警报选择。很快,我会做类似的事情:

 @IBAction func showAlertButtonTapped(_ sender: UIButton) {

    // create the alert
    let alert = UIAlertController(title: "Hi there", message: "You have three selections to choose from", preferredStyle: UIAlertControllerStyle.alert)

    // the actions and handler to decide what happens when user clicks on it
    alert.addAction(UIAlertAction(title: "Selection 1", style: UIAlertActionStyle.default, handler: nil))
    alert.addAction(UIAlertAction(title: "Selection 2", style: UIAlertActionStyle.default, handler: nil))
    alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))

    // show the alert
    self.present(alert, animated: true, completion: nil)
}

我如何在 android studio 中做到这一点?任何帮助表示赞赏。以下是我尝试过的,但在此之后卡住了,因为我无法发出另一个警报

AlertDialog alertDialog = new AlertDialog.Builder(MyActivity.this).create();
    alertDialog.setTitle("Hey there!");
    alertDialog.setMessage("You have three selections to choose from");
    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Selection 1",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                }
            });
    alertDialog.show();

【问题讨论】:

    标签: android


    【解决方案1】:

    这是带有 3 个按钮的警报对话框

        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", new DialogInterface.OnClickListener() {
    
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
    
                }
            });
    
            // create and show the alert dialog
            AlertDialog dialog = builder.create();
            dialog.show();
        }
    }
    

    【讨论】:

    • 谢谢,知道了!所以“null”,我会用我打算在用户单击选择时运行的代码来替换是吗?
    • yes 将 null 替换为:new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO 自动生成的方法存根 } }
    【解决方案2】:

    也许这段代码可以帮助你。

        AlertDialog.Builder b=  new  AlertDialog.Builder(getActivity())
    .setTitle("Enter Players")
    .setPositiveButton("OK",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                // do something...
            }
        }
    )
    .setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.dismiss();
            }
        }
    );
    

    取自:Adding positive / negative Button to DialogFragment's Dialog

    【讨论】:

    • 嗨!谢谢你的回复!我确实尝试过查看这个,但在这个中,只有 2 个选择,有什么方法可以添加第三个吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2016-02-08
    • 1970-01-01
    相关资源
    最近更新 更多