android – 具有确定/取消按钮的AlertDialog

前端之家收集整理的这篇文章主要介绍了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();

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

解决方法

您可以使用AlertDialog.Builder.setPositiveButton和AlertDialog.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();

猜你在找的Android相关文章