【问题标题】:Custom Dialog: Reference items in Custom View自定义对话框:自定义视图中的参考项目
【发布时间】:2014-07-21 16:18:56
【问题描述】:

我不知道如何在自定义对话框中引用项目。我看过hereherehere 和其他地方。 我无法让自定义对话框中的复选框在 onclick 事件中被引用,或者当我尝试直接在正按钮 onClick 中调用它时

XML:

<TextView
    android:id="@+id/dialog_title"
    style="?android:attr/textAppearanceLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/White"
    android:layout_weight="1"
    android:text="@string/delete_db_on_exit"
    android:textColor="@color/Black" />

<TextView
    android:id="@+id/dialog_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/White"
    android:text="@string/confirm_delete"
    android:textColor="@color/Black" />

<CheckBox
    android:id="@+id/confirm_delete_checkbox"
    android:paddingTop="5dp"
    android:background="@color/White"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/confirm_delete_checkbox"/>

Activity中调用对话框的代码:

private LayoutInflater inflator;
private View dialogView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_administration);
    inflator = this.getLayoutInflater();
    dialogView = inflator.inflate(R.layout.custom_dialog, null);
}

public void deleteDatabases(View view){
    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this)
            .setTitle(R.string.delete_db_on_exit)
            .setCustomTitle(getLayoutInflater().inflate(R.layout.custom_dialog, null))
            .setPositiveButton(R.string.confirm_button_text, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    if (((CheckBox) dialogView.findViewById(R.id.confirm_delete_checkbox)).isChecked()) { //Never true. No exceptions thrown
                        setDeleteDatabaseOnExit();
                        dialog.cancel();
                        //Navigate back to main activity
                        Intent mainActivityIntent = new Intent(AdministrationActivity.this, MainActivity.class);
                        startActivityForResult(mainActivityIntent, 1);
                    }
                }
            });

    CheckBox confirmDeleteCheckbox = (CheckBox)dialogView.findViewById(R.id.confirm_delete_checkbox);
    confirmDeleteCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int x = 0;
            x = 1;
        }
    });

    AlertDialog dialog = dialogBuilder.create();
    dialog.show();
}

【问题讨论】:

    标签: android


    【解决方案1】:

    变化:

            .setCustomTitle(getLayoutInflater().inflate(R.layout.custom_dialog, null))
    

    到:

            .setCustomTitle(dialogView)
    

    在您的对话框中,您正在创建一个新的 custom_dialog 视图,该视图也有一个复选框,但与 dialogView 中的不同。这就是为什么当你检查它为真时,对话框集中的那个是它自己为真,但 dialogView 中的那个仍然是假的。

    【讨论】:

    • 这似乎解决了问题!感谢那!我已经尝试了好几个小时了
    猜你喜欢
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    相关资源
    最近更新 更多