Android:在创建时关闭对话框强制

时间:2012-06-17 22:42:44

标签: android alertdialog

目标是按下menu_key上显示的对话框,但它会保持强制关闭。我咨询了官方FAQ on Dialogs,但没有运气。一切都按照那里的说明完成,但按下按钮后仍然会失败。这是对话框的创建:

static final int Choice_ID = 0;

    protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;
        switch(id) {
        case Choice_ID:
            // do the work to define the pause Dialog
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("TEST DIALOG")
                   .setCancelable(true);

            AlertDialog alert = builder.create();
            break;

        //default:
            //dialog = null;
        }
        return dialog;
    }

关于展示的部分看起来像这样:

public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_MENU) {
            showDialog(Choice_ID);

        };
};

1 个答案:

答案 0 :(得分:1)

这不会导致力量关闭,但如果您尝试设置dialog的值,则可以看到菜单:

dialog = builder.create();

如:

protected Dialog onCreateDialog(int id) {
    Dialog dialog = null;
    switch(id) {
    case Choice_ID:
        // do the work to define the pause Dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("TEST DIALOG")
               .setCancelable(true);

        dialog = builder.create();
        break;

    //default:
        //dialog = null;
    }
    return dialog;
}