0

当单击 listView 中的项目时,我有这段代码显示警报对话框;一切正常,直到我添加了一个复选框首选项,该首选项应该使另一个对话框能够输入文本字符串(稍后使用)。

AlertDialog.Builder helpBuilder = new AlertDialog.Builder(MyActivity.this);

helpBuilder.setIcon(android.R.drawable.ic_dialog_info);
adb.setTitle("title");
adb.setMessage("message");

        helpBuilder.setPositiveButton("positive", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

                        boolean fileRenameEnabled = settings.getBoolean("text_view", false);
                        if (fileRenameEnabled == true) {
                            AlertDialog.Builder adb = new AlertDialog.Builder(MyActivity.this);
                            LayoutInflater adbInflater = LayoutInflater.from(MyActivity.this);
                            View inputFilename = adbInflater.inflate(R.layout.dialog_text_view, null);
                            tv = (TextView) inputFilename.findViewById(R.id.tv);
                            tv.setHint("hint");

                            adb.setView(tv);
                            adb.setTitle("New filename");

                            adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    //some code here modifies method1()
                                    method1();
                                }
                            });
                            adb.show();
                        } else {
                            method1();
                        }
                    }
                });

                helpBuilder.setNeutralButton("neutral", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {

                        Checkbox1Enabled = settings.getBoolean("checkbox1", true);
                        if (Checkbox1Enabled == true) {
                            AlertDialog.Builder adb = new AlertDialog.Builder(MyActivity.this);
                            LayoutInflater adbInflater = LayoutInflater.from(MyActivity.this);
                            View info = adbInflater.inflate(R.layout.dialog_checkbox1, null);
                            cb = (CheckBox) info.findViewById(R.id.cb);
                            cb.setChecked(true);
                            adb.setView(info);
                            adb.setTitle("title");
                            adb.setMessage("message");

                            adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    if (cb.isChecked() == false) {
                                        SharedPreferences.Editor editor = settings.edit();
                                        editor.putBoolean("checkbox1", false);
                                        editor.commit();
                                        Checkbox1Enabled = settings.getBoolean("checkbox1", true);
                                        Log.d(DEBUG_TAG, "Checkbox1Enabled: " + Checkbox1Enabled);
                                    }
                                    method2(); 
                            }
                            });
                            adb.show();
                        } else {
                            method2();
                        }
                    }
                });
                AlertDialog helpDialog = helpBuilder.create();
                helpDialog.show();
            }
});

}

事实是我在onClickListener中性按钮内部做几乎相同的事情;唯一的区别是我在AlertDialog.Builder而不是在复选框中膨胀了一个 textView (有效......)。

例外是:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

任何帮助深表感谢。谢谢!

4

1 回答 1

0

您将添加TextView作为对话框的内容而不是膨胀的布局(并且TextView(很可能)在您膨胀的布局中已经有一个父级)。它应该是:

View inputFilename = adbInflater.inflate(R.layout.dialog_text_view, null);
tv = (TextView) inputFilename.findViewById(R.id.tv);
tv.setHint("hint");
adb.setView(inputFilename);
于 2012-12-10T16:57:30.900 回答