根据特定条件,我可能需要显示一个对话框。在应用程序可以继续之前,必须解决此情况。基本上,我需要“暂停”我的程序的执行,直到这个对话框被销毁。我尝试了多种不同的方法,但遇到了应用程序继续执行的问题。目前,showDialog 在我的主要 Activity 的 onCreate() 方法中。
如有任何建议,我们将不胜感激。
最佳答案
如果您只想在按下按钮后运行代码,您应该使用“onClickListener”:
参见:http://developer.android.com/reference/android/app/AlertDialog.html#setButton(int , java.lang.CharSequence, android.content.DialogInterface.OnClickListener)
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.first_run_version_title)
.setNeutralButton(R.string.ok_menu_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// code to run here
}
});
AlertDialog alert = builder.create();
alert.show(); // <-- Forgot this in the original post
关于android - 在 onCreate() 中显示对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4300012/