我的应用程序中有很多活动和对话框片段。我搜索了关于检测活动的信息,并了解了onUserInterAction功能,但它只检测活动,而不是在对话框片段打开时。
onUserInterAction
gjmwrych1#
我通过在对话框的Window回调上创建一个 Package 器来解决这个问题。YourDialogFragment.java
@Override public void onStart() { super.onStart(); UserInteractionInterceptor.wrapWindowCallback(getDialog().getWindow(), getActivity()); }
UserInteractionInterceptor.java
public class UserInteractionInterceptor { public static void wrapWindowCallback(Window window, FragmentActivity activity) { Window.Callback originalCallback = window.getCallback(); window.setCallback(new Window.Callback() { @Override public boolean dispatchKeyEvent(KeyEvent event) { return originalCallback.dispatchKeyEvent(event); } @Override public boolean dispatchKeyShortcutEvent(KeyEvent event) { return originalCallback.dispatchKeyShortcutEvent(event); } //This is the important override @Override public boolean dispatchTouchEvent(MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN) { if(activity != null) activity.onUserInteraction(); } return originalCallback.dispatchTouchEvent(event); } @Override public boolean dispatchTrackballEvent(MotionEvent event) { return originalCallback.dispatchTrackballEvent(event); } @Override public boolean dispatchGenericMotionEvent(MotionEvent event) { return originalCallback.dispatchGenericMotionEvent(event); } @Override public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { return originalCallback.dispatchPopulateAccessibilityEvent(event); } @Nullable @Override public View onCreatePanelView(int featureId) { return originalCallback.onCreatePanelView(featureId); } @Override public boolean onCreatePanelMenu(int featureId, Menu menu) { return originalCallback.onCreatePanelMenu(featureId, menu); } @Override public boolean onPreparePanel(int featureId, View view, Menu menu) { return originalCallback.onPreparePanel(featureId, view, menu); } @Override public boolean onMenuOpened(int featureId, Menu menu) { return originalCallback.onMenuOpened(featureId, menu); } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { return originalCallback.onMenuItemSelected(featureId, item); } @Override public void onWindowAttributesChanged(WindowManager.LayoutParams attrs) { originalCallback.onWindowAttributesChanged(attrs); } @Override public void onContentChanged() { originalCallback.onContentChanged(); } @Override public void onWindowFocusChanged(boolean hasFocus) { originalCallback.onWindowFocusChanged(hasFocus); } @Override public void onAttachedToWindow() { originalCallback.onAttachedToWindow(); } @Override public void onDetachedFromWindow() { originalCallback.onDetachedFromWindow(); } @Override public void onPanelClosed(int featureId, Menu menu) { originalCallback.onPanelClosed(featureId, menu); } @Override public boolean onSearchRequested() { return originalCallback.onSearchRequested(); } @TargetApi(Build.VERSION_CODES.M) @Override public boolean onSearchRequested(SearchEvent searchEvent) { return originalCallback.onSearchRequested(searchEvent); } @Nullable @Override public ActionMode onWindowStartingActionMode(ActionMode.Callback callback) { return originalCallback.onWindowStartingActionMode(callback); } @TargetApi(Build.VERSION_CODES.M) @Nullable @Override public ActionMode onWindowStartingActionMode(ActionMode.Callback callback, int type) { return originalCallback.onWindowStartingActionMode(callback, type); } @Override public void onActionModeStarted(ActionMode mode) { originalCallback.onActionModeStarted(mode); } @Override public void onActionModeFinished(ActionMode mode) { originalCallback.onActionModeFinished(mode); } }); }
}
drkbr07n2#
这对我很有效,Kotlin版本:在对话框中:
override fun onStart() { super.onStart() UserInteractionInterceptor.wrapWindowCallback(dialog.window, activity) }
和UserInteractionInterceptor类别:
object UserInteractionInterceptor { fun wrapWindowCallback(window: Window, activity: FragmentActivity?) { val originalCallback: Window.Callback = window.callback window.callback = object : Window.Callback { override fun dispatchKeyEvent(event: KeyEvent?): Boolean { return originalCallback.dispatchKeyEvent(event) } override fun dispatchKeyShortcutEvent(event: KeyEvent?): Boolean { return originalCallback.dispatchKeyShortcutEvent(event) } //This is the important override override fun dispatchTouchEvent(event: MotionEvent): Boolean { if (event.action == MotionEvent.ACTION_DOWN) { activity?.onUserInteraction() } return originalCallback.dispatchTouchEvent(event) } override fun dispatchTrackballEvent(event: MotionEvent?): Boolean { return originalCallback.dispatchTrackballEvent(event) } override fun dispatchGenericMotionEvent(event: MotionEvent?): Boolean { return originalCallback.dispatchGenericMotionEvent(event) } override fun dispatchPopulateAccessibilityEvent(event: AccessibilityEvent?): Boolean { return originalCallback.dispatchPopulateAccessibilityEvent(event) } @Nullable override fun onCreatePanelView(featureId: Int): View? { return originalCallback.onCreatePanelView(featureId) } override fun onCreatePanelMenu(featureId: Int, p1: Menu): Boolean { return originalCallback.onCreatePanelMenu(featureId, p1) } override fun onPreparePanel(featureId: Int, view: View?, p2: Menu): Boolean { return originalCallback.onPreparePanel(featureId, view, p2) } override fun onMenuOpened(featureId: Int, p1: Menu): Boolean { return originalCallback.onMenuOpened(featureId, p1) } override fun onMenuItemSelected(featureId: Int, p1: MenuItem): Boolean { return originalCallback.onMenuItemSelected(featureId, p1) } override fun onWindowAttributesChanged(attrs: WindowManager.LayoutParams?) { originalCallback.onWindowAttributesChanged(attrs) } override fun onContentChanged() { originalCallback.onContentChanged() } override fun onWindowFocusChanged(hasFocus: Boolean) { originalCallback.onWindowFocusChanged(hasFocus) } override fun onAttachedToWindow() { originalCallback.onAttachedToWindow() } override fun onDetachedFromWindow() { originalCallback.onDetachedFromWindow() } override fun onPanelClosed(featureId: Int, p1: Menu) { originalCallback.onPanelClosed(featureId, p1) } override fun onSearchRequested(): Boolean { return originalCallback.onSearchRequested() } @TargetApi(Build.VERSION_CODES.M) override fun onSearchRequested(searchEvent: SearchEvent?): Boolean { return originalCallback.onSearchRequested(searchEvent) } @Nullable override fun onWindowStartingActionMode(callback: ActionMode.Callback?): ActionMode? { return originalCallback.onWindowStartingActionMode(callback) } @TargetApi(Build.VERSION_CODES.M) @Nullable override fun onWindowStartingActionMode(callback: ActionMode.Callback?, type: Int): ActionMode? { return originalCallback.onWindowStartingActionMode(callback, type) } override fun onActionModeStarted(mode: ActionMode?) { originalCallback.onActionModeStarted(mode) } override fun onActionModeFinished(mode: ActionMode?) { originalCallback.onActionModeFinished(mode) } } }
2条答案
按热度按时间gjmwrych1#
我通过在对话框的Window回调上创建一个 Package 器来解决这个问题。
YourDialogFragment.java
UserInteractionInterceptor.java
}
drkbr07n2#
这对我很有效,Kotlin版本:
在对话框中:
和UserInteractionInterceptor类别:
}