onUserInteraction在DialogPreference中不起作用

时间:2013-02-12 09:00:46

标签: android dialog dialog-preference

预计会为任何用户交互调用onUserInteraction。它在PreferenceActivity中工作正常。但是,当弹出DialogPreference时,即使存在用户交互(例如触摸事件),也不会再调用onUserInteraction

似乎DialogPreference并非唯一的情况。每当显示Dialog时,它都不会报告用户与活动的互动。

但如果我真的需要它,我该怎么办?谢谢。

3 个答案:

答案 0 :(得分:6)

据我所知,当用户与对话框进行交互时,根本不会调用onUserInteraction()(甚至从您正在监视交互的Activity开始)。

我知道的两个解决方案是:

  • 子类Dialog / DialogPreference类并覆盖dispatchTouchEvent()

  • 实现Window.Callback接口并通过发出以下内容将其设置为Dialog窗口回调:

    dialog.getWindow().setCallback(callbackImplementation);
    

    注意:此实现应通过调用适当的对话方法处理所有收到的事件,或以您自己的方式处理事件(例如,通过手动调用onUserInteraction())。

修改

您可以通过多种方式从自定义Activity实例中获取PreferenceDialog

  1. 调用返回DialogPreference.getPreferenceManager()的{​​{1}}方法。它有PreferenceManager方法,但它是包私有,因此您必须将自定义getActivity()放入DialogPreference包中才能访问它。

    < / LI>
  2. android.preference中,在充实首选项后,使用PreferenceActivity.onCreate()按键查找自定义findPreference()。然后将其投放到自定义类,并通过访问者将活动设置为DialogPreference

  3. 我会选择第二种选择。

答案 1 :(得分:3)

以下是DialogFragment的完整解决方案,触摸时触发Activity的onUserInteraction()并保留默认回调的行为:

public abstract class BaseDialogFragment extends DialogFragment {

    @Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final Window window = getDialog().getWindow();
        if (window != null) {
            window.setCallback(new UserInteractionAwareCallback(window.getCallback(), getActivity()));
        }
    }
}

这是Callback本身:

public class UserInteractionAwareCallback implements Window.Callback {

    private final Window.Callback originalCallback;
    private final Activity activity;

    public UserInteractionAwareCallback(final Window.Callback originalCallback, final Activity activity) {
        this.originalCallback = originalCallback;
        this.activity = activity;
    }

    @Override
    public boolean dispatchKeyEvent(final KeyEvent event) {
        return originalCallback.dispatchKeyEvent(event);
    }

    @Override
    public boolean dispatchKeyShortcutEvent(final KeyEvent event) {
        return originalCallback.dispatchKeyShortcutEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(final MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
                if (activity != null) {
                    activity.onUserInteraction();
                }
                break;
            default:
        }
        return originalCallback.dispatchTouchEvent(event);
    }

    @Override
    public boolean dispatchTrackballEvent(final MotionEvent event) {
        return originalCallback.dispatchTrackballEvent(event);
    }

    @Override
    public boolean dispatchGenericMotionEvent(final MotionEvent event) {
        return originalCallback.dispatchGenericMotionEvent(event);
    }

    @Override
    public boolean dispatchPopulateAccessibilityEvent(final AccessibilityEvent event) {
        return originalCallback.dispatchPopulateAccessibilityEvent(event);
    }

    @Nullable
    @Override
    public View onCreatePanelView(final int featureId) {
        return originalCallback.onCreatePanelView(featureId);
    }

    @Override
    public boolean onCreatePanelMenu(final int featureId, final Menu menu) {
        return originalCallback.onCreatePanelMenu(featureId, menu);
    }

    @Override
    public boolean onPreparePanel(final int featureId, final View view, final Menu menu) {
        return originalCallback.onPreparePanel(featureId, view, menu);
    }

    @Override
    public boolean onMenuOpened(final int featureId, final Menu menu) {
        return originalCallback.onMenuOpened(featureId, menu);
    }

    @Override
    public boolean onMenuItemSelected(final int featureId, final MenuItem item) {
        return originalCallback.onMenuItemSelected(featureId, item);
    }

    @Override
    public void onWindowAttributesChanged(final WindowManager.LayoutParams attrs) {
        originalCallback.onWindowAttributesChanged(attrs);
    }

    @Override
    public void onContentChanged() {
        originalCallback.onContentChanged();
    }

    @Override
    public void onWindowFocusChanged(final boolean hasFocus) {
        originalCallback.onWindowFocusChanged(hasFocus);
    }

    @Override
    public void onAttachedToWindow() {
        originalCallback.onAttachedToWindow();
    }

    @Override
    public void onDetachedFromWindow() {
        originalCallback.onDetachedFromWindow();
    }

    @Override
    public void onPanelClosed(final int featureId, final Menu menu) {
        originalCallback.onPanelClosed(featureId, menu);
    }

    @Override
    public boolean onSearchRequested() {
        return originalCallback.onSearchRequested();
    }

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public boolean onSearchRequested(final SearchEvent searchEvent) {
        return originalCallback.onSearchRequested(searchEvent);
    }

    @Nullable
    @Override
    public ActionMode onWindowStartingActionMode(final ActionMode.Callback callback) {
        return originalCallback.onWindowStartingActionMode(callback);
    }

    @TargetApi(Build.VERSION_CODES.M)
    @Nullable
    @Override
    public ActionMode onWindowStartingActionMode(final ActionMode.Callback callback, final int type) {
        return originalCallback.onWindowStartingActionMode(callback, type);
    }

    @Override
    public void onActionModeStarted(final ActionMode mode) {
        originalCallback.onActionModeStarted(mode);
    }

    @Override
    public void onActionModeFinished(final ActionMode mode) {
        originalCallback.onActionModeFinished(mode);
    }
}

答案 2 :(得分:0)

这是一个更独立、更完整的 Kotlin 实现:

/**
 * Sets up the receiver's [window][Dialog.getWindow] to call [Activity.onUserInteraction]
 * at appropriate times, mirroring the calls made in [Activity] itself.
 * This method should be called immediately after [Dialog.show].
 */
fun Dialog.reportUserInteraction() {
  window?.let { window ->
    val activity = window.decorView.activity

    val wrappedCallback = window.callback
    window.callback = object : Window.Callback by wrappedCallback {
      override fun dispatchGenericMotionEvent(event: MotionEvent): Boolean {
        activity.onUserInteraction()
        return wrappedCallback.dispatchGenericMotionEvent(event)
      }

      override fun dispatchKeyEvent(event: KeyEvent): Boolean {
        activity.onUserInteraction()
        return wrappedCallback.dispatchKeyEvent(event)
      }

      override fun dispatchKeyShortcutEvent(event: KeyEvent): Boolean {
        activity.onUserInteraction()
        return wrappedCallback.dispatchKeyShortcutEvent(event)
      }

      override fun dispatchTouchEvent(event: MotionEvent): Boolean {
        if (event.action == ACTION_DOWN) activity.onUserInteraction()
        return wrappedCallback.dispatchTouchEvent(event)
      }

      override fun dispatchTrackballEvent(event: MotionEvent): Boolean {
        activity.onUserInteraction()
        return wrappedCallback.dispatchTrackballEvent(event)
      }
    }
  }
}

依赖:

val View.activity: Activity
  get() = context.activityOrNull!!

val Context.activityOrNull: Activity?
  get() {
    var context = this
    while (true) {
      if (context is Application) {
        return null
      }
      if (context is Activity) {
        return context
      }
      if (context is ContextWrapper) {
        val baseContext = context.baseContext
        // Prevent Stack Overflow.
        if (baseContext === this) {
          return null
        }
        context = baseContext
      } else {
        return null
      }
    }
  }