web-dev-qa-db-ja.com

警告ダイアログ2つのボタン

こんにちは、私は単純な問題があり、alertDialogがあり、ここで検索した2つのボタンを表示したいのですが、以前のオプションは機能しなくなり、廃止されました。

これを行う新しい方法を知っている人は誰でも、以下の私のコードが動作しないのを見ることができます。

  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!


    }
    });
35
Matt

これを試して

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

ボタンの追加

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();
78
Joe

これはあなたのためのトリックを行う必要があります:

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!
  }
});
10
kaspermoerch
btn_cancle.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        AlertDialog.Builder alert = new AlertDialog.Builder(inflater.getContext());
        alert.setTitle("Do you want to Reject request");
        alert.setIcon(Android.R.drawable.ic_dialog_alert);
        alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                Toast.makeText(inflater.getContext(), "Rejected", Toast.LENGTH_SHORT).show();
            } });


        adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
               // finish();
            } });
        adb.show();
       // Toast.makeText(inflater.getContext(), "Hello", Toast.LENGTH_SHORT).show();
    }
0
DEEP ADHIYA

アラートダイアログビルダーには、setButton2およびsetButton3という追加のメソッドがあり、これらも使用できます。

0
rohitsakala
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();
0
Nicolas