带有确定/取消按钮的AlertDialog

sma*_*use 6 alert android button android-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();
Run Code Online (Sandbox Code Playgroud)

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

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