是否有Dialog或DialogFragment中Activity的dispatchTouchEvent()的等效项 [英] Is there an equivalent for dispatchTouchEvent() from Activity in Dialog or DialogFragment

查看:536
本文介绍了是否有Dialog或DialogFragment中Activity的dispatchTouchEvent()的等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要拦截应用程序中的所有触摸事件,以监视自定义活动超时.

I need to intercept all touch events in the application to monitor for a custom activity time out.

当前,我在活动中使用dispatchTouchEvent(),但是如果我在屏幕上有一个对话框,则不会调用它.有没有人知道我是否可以通过出现对话框的方式来实现相同的功能?

Currently I use dispatchTouchEvent() in my activities but this is not called if I have a dialog on the screen. Does any one know if there any way I can have this same functionality with a dialog being present?

谢谢

推荐答案

要在DialogFragment中使用dispatchTouchEvent(),请覆盖onCreateDialog并使用dispatchTouchEvent返回自定义Dialog(在您的自定义DialogFragment中).

For use dispatchTouchEvent() in DialogFragment, override onCreateDialog and return a custom Dialog with dispatchTouchEvent (in your custom DialogFragment).

例如,用于在DialogFragment中单击外部时关闭键盘:

Exemple, for dismiss keyboard when click outside in DialogFragment:

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new Dialog(getActivity(), getTheme()) {
        @Override
        public boolean dispatchTouchEvent(@NonNull MotionEvent motionEvent) {
            if (getCurrentFocus() != null) {
                InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            }
            return super.dispatchTouchEvent(motionEvent);
        }

    };
}

这篇关于是否有Dialog或DialogFragment中Activity的dispatchTouchEvent()的等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆