Android 创建UIAlert

Android 创建UIAlert,android,Android,我是安卓工作室的新手,以前是用swift编写代码的。我曾尝试在android studio中创建一个警报视图,但大多数都没有给我超过2个警报选择。在swift中,我会做如下工作: @IBAction func showAlertButtonTapped(_ sender: UIButton) { // create the alert let alert = UIAlertController(title: "Hi there", message: "You have thr

我是安卓工作室的新手,以前是用swift编写代码的。我曾尝试在android studio中创建一个警报视图,但大多数都没有给我超过2个警报选择。在swift中,我会做如下工作:

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

也许这段代码可以帮你

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

摘自:

也许截取的代码可以帮助您

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

摘自:

这是带有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();
    }
}

这是带有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();
    }
}

你好谢谢回复!我确实试过研究这个,但在这个,只有2个选择,有没有办法我可以添加第三个?嗨!谢谢回复!我确实试过研究这个,但在这个,只有2个选择,有没有办法我可以添加第三个?谢谢,明白了!所以“null”,我将替换为我打算在用户单击选择时运行的代码yea?yes将null替换为:new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog,int which){//TODO自动生成的方法存根}谢谢,明白了!所以“null”,我将替换为我打算在用户单击选择时运行的代码yea?yes将null替换为:new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog,int which){//TODO自动生成的方法存根}