当用户触摸对话框片段外部时,我希望执行一些任务。我该如何做?
在我的自定义对话框片段中,有一段代码可以防止在触摸外部时关闭对话框:
getDialog().setCanceledOnTouchOutside(false);
当用户触摸对话框片段外部时,我希望执行一些任务。我该如何做?
在我的自定义对话框片段中,有一段代码可以防止在触摸外部时关闭对话框:
getDialog().setCanceledOnTouchOutside(false);
那么您需要移除getDialog().setCanceledOnTouchOutside(false);
并使用一些反射来利用Window
的隐藏方法来决定何时关闭对话框:
最初的回答。
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return object : Dialog(activity as Context, theme) {
override fun onTouchEvent(event: MotionEvent): Boolean {
if (dialogShouldCloseOnTouch(window, context, event)) {
// do custom logic
return true
} else {
return super.onTouchEvent(event)
}
}
}
}
private fun dialogShouldCloseOnTouch(
window: Window,
context: Context,
event: MotionEvent): Boolean {
val method = window.javaClass.getMethod("shouldCloseOnTouch", Context::class.java, MotionEvent::class.java)
return method.invoke(window, context, event) as Boolean
}
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Make us non-modal, so that others can receive touch events.
getWindow().setFlags(LayoutParams.FLAG_NOT_TOUCH_MODAL, LayoutParams.FLAG_NOT_TOUCH_MODAL);
// ...but notify us that it happened.
getWindow().setFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
// Note that flag changes must happen *before* the content view is set.
setContentView(R.layout.my_dialog_view);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// If we've received a touch notification that the user has touched
// outside the app, finish the activity.
if (MotionEvent.ACTION_OUTSIDE == event.getAction()) {
//outside touch event
return true;
}
// Delegate everything else to Activity.
return super.onTouchEvent(event);
}
}
参考:在这里看第二个答案。
P.S. 在问题中,他说他有一个带有对话框主题的活动。