创建UIAlert

时间:2018-08-12 10:38:00

标签: android

我是Android Studio的新手,以前是快速编写代码的。我试图研究如何在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();

2 个答案:

答案 0 :(得分:1)

也许这段代码被窃取可以帮助您。

    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

答案 1 :(得分: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();
    }
}
相关问题