0

我有一个自定义对话框(扩展对话框),其内容视图是自定义视图组。视图组有几个 edittext 子项,但我自己在视图组的 dispatchDraw 和 onTouch 方法中处理按钮的绘制和单击(我试图避免膨胀尽可能多的视图)。具体来说:这个视图没有我可以设置为对话框的关闭按钮的按钮子项。我想从 viewgroup 的 onTouch 方法中关闭对话框,但除了模拟按下后退键之外,我不知道该怎么做。

活动代码:

public class My_Activity extends Activity {
    ...
    public void onCreate(Bundle savedInstanceState) {
        ...
        //if there's no Class_That_Im_Editing in the database, prompt the user to make a new one by adding information to the editviews in this custom dialog and clicking the area where I draw the ok button
        my_dialog = new Custom_Dialog(this, R.style.CustomDlg, new Class_That_Im_Editing());
    }
}

对话代码:

public class Custom_Dialog extends Dialog {
    ...
        public void onCreate(Bundle savedInstanceState) {
            ...
            setContentView(new Custom_ViewGroup(context, Class_That_Im_Editing));
        }
}

视图组代码:

public class Custom_ViewGroup extends ViewGroup implements OnTouchListener {
    //this class has some edittext children but _no_ buttons
    ...
    public boolean onTouch(View view, MotionEvent event) {
        if ( logic checking if the user has clicked the button area ) {
            //??? what do I put here to dismiss the dialog
        }
    }
}

我能想到的唯一其他方法是使用dismissDialog(int) 方法,这意味着覆盖onCreateDialog 和onPrepareDialog 事件处理程序。但是如何从视图的 onTouch 方法中调用dismissDialog?

也许我需要设置某种监听器?如果是这样,执行此操作的骨架代码是什么?

4

1 回答 1

1

因此,当我不在对话框存在的范围内时,问题是告诉对话框关闭()。这是我的解决方案:

在与对话框相同的范围内创建 OnTouchListener - 在本例中,在我的主要活动中。然后在初始化对话框时传递它,这反过来又需要将它传递给视图组。

它看起来像这样:

活动代码:

public class My_Activity extends Activity {
    public Custom_Dialog my_dialog;
    ...
    public void onCreate(Bundle savedInstanceState) {
        OnTouchListener otl_custom_dialog = new OnTouchListener() {
            public boolean onTouch(View view, MotionEvent event) {
                if ( logic checking if the user has clicked the button area ) {
                    //notice I can still access any _public_ variable within the viewgroup class
                    //by using my_dialog.my_custom_viewgroup.public_variable
                    ...
                    //I can now call dismiss() from within this scope
                    my_dialog.dismiss();
                }
                ...
            }
        }
        ...
        //if there's no Class_That_Im_Editing in the database, prompt the user to make a new one by adding information to the editviews in this custom dialog and clicking the area where I draw the ok button
        my_dialog = new Custom_Dialog(this, R.style.CustomDlg, new Class_That_Im_Editing(), otl_custom_dialog);
        my_dialog.show();
    }
}

对话代码:

public class Custom_Dialog extends Dialog {
    Custom_ViewGroup my_custom_viewgroup;
    OnTouchListener otl_custom_dialog;
    ...
        public void onCreate(Bundle savedInstanceState) {
            ...
            setContentView(new Custom_ViewGroup(context, class_that_im_editing, otl_custom_dialog));
        }
}

视图组代码:

public class Custom_ViewGroup extends ViewGroup implements OnTouchListener {
    public Custom_ViewGroup(Context context, Class_That_Im_Editing class_that_im_editing, OnTouchListener otl_custom_dialog) {
        ...
        this.setOnTouchListener(otl_custom_dialog);
    }
}

我已经测试过这种方法,它工作正常。希望这对其他人有帮助!

于 2011-05-03T21:56:53.140 回答