警报对话框两个按钮

时间:2011-11-22 13:45:17

标签: android alertdialog

你好,我有一个简单的问题,我有一个alertDialog,我希望它显示我在这里搜索过的两个按钮,但似乎之前的选项不再工作,不推荐使用。

任何人都知道这样做的新方法,你可以看到下面的代码不起作用。

  Button share = (Button) findViewById(R.id.btn_share);
    share.setOnClickListener(new OnClickListener() {   
        public void onClick(View v) {
           // call some other methods before that I guess...
             AlertDialog alertDialog = new AlertDialog.Builder(PasswActivity.this).create(); //Read Update
             alertDialog.setTitle("Uprgade");
             alertDialog.setMessage("Upgrade Text Here");

             alertDialog.setButton("Upgrade", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

             });
                 alertDialog.setButton("Cancel", new DialogInterface.OnClickListener()    {
                public void onClick(DialogInterface dialog, int which) {

             });



             alertDialog.show();  //<-- See This!


    }
    });

7 个答案:

答案 0 :(得分:72)

Adding Buttons

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
   .setCancelable(false)
   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            MyActivity.this.finish();
       }
   })
   .setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
       }
   });
AlertDialog alert = builder.create();
alert.show();

答案 1 :(得分:56)

试试这个

public void showDialog(Activity activity, String title, CharSequence message) {
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);

    if (title != null) builder.setTitle(title);

    builder.setMessage(message);
    builder.setPositiveButton("OK", null);
    builder.setNegativeButton("Cancel", null);
    builder.show();
}

答案 2 :(得分:10)

这应该适合你:

Button share = (Button) findViewById(R.id.btn_share);
share.setOnClickListener(new OnClickListener() {   
  public void onClick(View v) {
    // call some other methods before that I guess...
    AlertDialog alertDialog = new AlertDialog.Builder(PasswActivity.this).create(); //Read Update
    alertDialog.setTitle("Uprgade");
    alertDialog.setMessage("Upgrade Text Here");
    alertDialog.setButton( Dialog.BUTTON_POSITIVE, "Upgrade", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int which) {

       });

    alertDialog.setButton( Dialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener()    {
      public void onClick(DialogInterface dialog, int which) {

      });

    alertDialog.show();  //<-- See This!
  }
});

答案 3 :(得分:0)

AlertDialog.Builder adb = new AlertDialog.Builder(this);


adb.setView(alertDialogView);


adb.setTitle("Title of alert dialog");


adb.setIcon(android.R.drawable.ic_dialog_alert);


adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {


        EditText et = (EditText)alertDialogView.findViewById(R.id.EditText1);


        Toast.makeText(Tutoriel18_Android.this, et.getText(), Toast.LENGTH_SHORT).show();
  } });


adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {

        finish();
  } });
adb.show();

答案 4 :(得分:0)

警报对话框构建器有一个名为setButton2和setButton3的附加方法,也可以使用!

答案 5 :(得分:0)

$ZZ5

答案 6 :(得分:0)

Kotlin 解决方案:

val alertDialog: AlertDialog = AlertDialog.Builder(this@ImageViewerActivity)
                                .create()

                        alertDialog.setTitle("Uprgade");
                        alertDialog.setMessage("Upgrade Text Here");

                        alertDialog.setButton(Dialog.BUTTON_POSITIVE, "Upgrade", DialogInterface.OnClickListener { dialog, which ->


                        });
                        alertDialog.setButton(Dialog.BUTTON_NEGATIVE, "Cancel", DialogInterface.OnClickListener { dialog, which ->


                        });
                        alertDialog.show();