带有确定/取消按钮的AlertDialog

时间:2014-08-26 11:13:15

标签: android button alert alertdialog

我创建了这个AlertDialog:

            String msg = "Connessione lenta o non funzionante";
            AlertDialog alertDialog;
            alertDialog = new AlertDialog.Builder(HomePage.this).create();
            alertDialog.setTitle("Timeout connessione");
            alertDialog.setMessage(msg);
            alertDialog.show();

我想添加确定和取消按钮。我在这里搜索StackOverflow但是setButton方法似乎已被弃用。我还为AlertDialog.Builder找到了setPositiveButton和setNegativeButton,但即使它们似乎也被弃用了。

4 个答案:

答案 0 :(得分:16)

您可以使用AlertDialog.Builder.setPositiveButtonAlertDialog.Builder.setNegativeButton,两者都不会弃用(see the documentation):

new AlertDialog.Builder(HomePage.this)
        .setTitle("Timeout connessione")
        .setMessage("Connessione lenta o non funzionante")
        .setNegativeButton(android.R.string.cancel, null) // dismisses by default
        .setPositiveButton(android.R.string.ok, new OnClickListener() {
            @Override public void onClick(DialogInterface dialog, int which) {
                // do the acknowledged action, beware, this is run on UI thread
            }
        })
        .create()
        .show();

答案 1 :(得分:3)

使用alertDialog.setPositiveButtonalertDialog.setNegativeButton,以下是您可以使用的实用程序方法:

public static void ask(final Activity activity, String title, String msg,
                       DialogInterface.OnClickListener okListener, 
                       DialogInterface.OnClickListener cancelListener) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
    alertDialog.setTitle(title);
    alertDialog.setMessage(msg);
    alertDialog.setPositiveButton(R.string.ok, okListener);
    alertDialog.setNegativeButton(R.string.cancel, cancelListener);
    alertDialog.show();
}

您可以这样称呼它:

ask(mInstance, getString(R.string.app_name),
            getString(R.string.confirm_text_here),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) { // OK
                    // do Something
                }
            }, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) { // Cancel
                    // do Something
                }
            });

有关详细信息,请参阅Android Docs

答案 2 :(得分:0)

I usually use this code and It works perfectly use this  

  DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which){
                    case DialogInterface.BUTTON_POSITIVE:
                        finish();
                        break;

                    case DialogInterface.BUTTON_NEGATIVE:
                        break;
                    }
                }
            };

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Want To Do Some Task").setPositiveButton("Ok"), dialogClickListener)
            .setNegativeButton("Cancel"), dialogClickListener).show();

答案 3 :(得分:0)

试试这段代码我认为它会完美运作。

public void open(View view){
  AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
  alertDialogBuilder.setTitle("alert dialog");
  alertDialogBuilder.setMessage("conform to delete");
  alertDialogBuilder.setPositiveButton("ok", 
  new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface arg0, int arg1) {
        Intent positveActivity = new                                                 Intent(getApplicationContext(),com.example.alertdialog.PositiveActivity.class);
        startActivity(positveActivity);

     }
  });
  alertDialogBuilder.setNegativeButton("cancel", 
  new DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface dialog, int which) {
        Intent negativeActivity = new Intent(getApplicationContext(),com.example.alertdialog.NegativeActivity.class);
        startActivity(negativeActivity);
     }
  });

  AlertDialog alertDialog = alertDialogBuilder.create();
  alertDialog.show();
    }