我有一个自定义对话框(扩展对话框),其内容视图是自定义视图组。视图组有几个 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?
也许我需要设置某种监听器?如果是这样,执行此操作的骨架代码是什么?