警报对话框两个按钮

Mat*_*att 36 android 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!


    }
    });
Run Code Online (Sandbox Code Playgroud)

Joe*_*Joe 80

添加按钮

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();
Run Code Online (Sandbox Code Playgroud)

  • +1,但请考虑在代码中添加`alert.show();`. (7认同)

rfs*_*010 59

试试这个

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();
}
Run Code Online (Sandbox Code Playgroud)

  • 我们需要将按钮声明为正面还是负面?如果他们在哪里怎么办?只有2种不同的选择?或者3? (5认同)

kas*_*rch 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!
  }
});
Run Code Online (Sandbox Code Playgroud)