带有确认/取消按钮的AlertDialog

7

我创建了这个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方法,但它们似乎也已经过时了。


2
弃用...何时如何? - nobalG
关于对话框的一切:http://developer.android.com/guide/topics/ui/dialogs.html - Phantômaxx
你为什么认为 setPositiveButton 和 setNegativeButton 已经过时了?事实上它们并没有过时。 - Opiatefuchs
只有 AlertDialog.setButton/setButton2/setButton3 被弃用,但是 Builder 上的 setButton(whichButton, ...) 和 Negative/Positive/Neutral 没有。 - TWiStErRob
4个回答

16

你可以使用AlertDialog.Builder.setPositiveButtonAlertDialog.Builder.setNegativeButton,两者都未被弃用(请查看文档):

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

有没有办法控制AlertDialog中负按钮和正按钮的顺序?比如说我想把取消按钮放在左边... - IgorGanapolsky
只需要反转标签吗? - Blackbelt
你是说否定按钮要用 string.ok,而肯定按钮要用 string.cancel 吗? - IgorGanapolsky
是的,除非你想使用已弃用的button_0 / 1按钮或具有自定义视图,否则我没有看到其他选择。 - Blackbelt

4

使用 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文档


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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接