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 に答える