自定义AlertDialog无法在onCreate()中初始化

时间:2011-12-31 10:41:50

标签: java android alertdialog oncreate

我正在尝试通过扩展AlertDialog类来构建自定义AlertDialog 像往常一样,我在onCreate()方法中设置对话框。或者,我正在尝试这样做:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.setTitle("Some title");
    this.setButton(BUTTON_POSITIVE, "Click me", (DialogInterface.OnClickListener)null);

    final FrameLayout custom = (FrameLayout) this
            .findViewById(android.R.id.custom);

    custom.addView(this.getLayoutInflater().inflate(R.layout.mydlg, null),
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

}

现在,在显示此对话框的实例时,会显示 nothing 当前Activity淡出并失去焦点但不显示对话框的单个像素。按返回Activity带回前台,向我显示实际显示 的对话框,但只显示一个完全空的对话框。< / p>

但是,当我创建AlertDialog并使用时,dlg.setButton(BUTTON_POSITIVE, "Click me", (DialogInterface.OnClickListener)null); 对话框显示相应的按钮 即使我在构造函数中使用与上面相同的代码设置我的自定义对话框,一切似乎都可以正常工作。

现在,这怎么可能?为什么我似乎无法在其onCreate()方法中初始化对话框?这不是你应该初始化任何 GUI元素的方式吗?我错过了什么?

修改

请注意,某些内容已“显示”,淡出活动并从中获取焦点。它只是看起来完全是空的/不可见的。

这是另一次尝试:

    this.setTitle("Some title");
    this.setButton(BUTTON_POSITIVE, "Click me", (DialogInterface.OnClickListener)null);

    final View v = this.getLayoutInflater().inflate(R.layout.mydlg, null);

    this.setView(v);

这些确切的行执行在放入对话框的构造函数时起作用 放入对话框onCreate()后,这些确切的行不会

这是怎么回事?!

一般来说,我不应该在onCreate()中这样做吗? - 如果我在构造函数中执行上述初始化,我是否会遇到麻烦? (无论如何,这对我来说似乎不太干净。)

3 个答案:

答案 0 :(得分:2)

您需要调用show()方法才能看到内容。

答案 1 :(得分:1)

您应该考虑使用AlertDialog.Builder而不是子类化AlertDialog本身。它允许您在示例中执行所需的所有操作(按顺序:setTitle(),setPositiveButton()和setView())。不要忘记在最后调用create()来实际获取对话框。

另外,检查您的onCreateDialog()和onPrepareDialog()活动方法是否正确实现。如果您根本没有实现它们(非托管对话框),请考虑这样做,特别是如果您的应用允许更改方向。您可能知道这一点,但这是一个教程:

http://developer.android.com/guide/topics/ui/dialogs.html

另外,DialogFragments实现这一点要简单一些,但您需要更新的API版本或兼容性软件包:

http://developer.android.com/reference/android/app/DialogFragment

最后一个问题 - 您在活动中调用show()在哪里? onResume()应该没问题,onCreate()没那么多。

答案 2 :(得分:1)

对不起,我迟到了。)

警告对话框必须采用不同的方式。

我这样做的方法是在创建警告对话框之前自定义视图:

// This is the activity that is the background of the AlertDialog
public class Main extends Activity {

    public static final int DIALOG_CONFIG = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.emptybackground);
    }

    @Override
    protected void onStart() {
        super.onStart();

        // Open the alert dialog on openning the Activity
        showDialog(Main.DIALOG_CONFIG );
    }

    protected Dialog onCreateDialog(int id) {
        LayoutInflater factory = LayoutInflater.from(this);
        switch (id) {
        case DIALOG_CONFIG:
            // Here, we load the existing view R.layout.config
            configView = factory.inflate(R.layout.config, null);
            configDialog = new AlertDialog.Builder(this)
                .setTitle("Configuration")
                .setView(configView)
                .create();

            // Using configView, you can do whatever you want with the view. Here, we add value to a spinner.
            Spinner spinner = (Spinner)configView.findViewById(R.id.config_select_conn);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            adapter.add("TCP");
            adapter.add("Bluetooth");
            spinner.setAdapter(adapter);

            return configPrinter;
        }
        return null;
    }
}