我创建了这个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();