在onCreate()中显示一个对话框

时间:2010-11-29 00:34:25

标签: android dialog

我可能需要显示一个对话框,具体取决于某个条件。在应用程序可以继续之前,必须解决此问题。基本上,我需要“暂停”我的程序的执行,直到该对话框被销毁。我尝试了一些不同的方法,但我遇到了应用程序继续执行的问题。目前,showDialog位于我主要活动的onCreate()方法中。

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:4)

如果只想在按下按钮后运行代码,则应使用`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
相关问题