问题描述
预计 onUserInteraction
将被调用以进行任何用户交互.它在 PreferenceActivity
中运行良好.但是,当弹出 DialogPreference
时,即使有触摸事件等用户交互,也不会再调用 onUserInteraction
.
It is expected that onUserInteraction
is being called for any user interaction. it works fine in PreferenceActivity
. However, when a DialogPreference
is popup, onUserInteraction
is not called anymore even there is user interaction such as touch event.
DialogPreference
似乎不是唯一的情况.每当显示 Dialog
时,它都不会将用户交互报告给活动.
It seems that DialogPreference
is not the only case. Whenever Dialog
is shown, it does not report the user interaction to activity.
但是,如果我真的需要它,我该怎么办.谢谢.
But what can I do if I really need it. Thank You.
推荐答案
据我所知,onUserInteraction()
在用户与对话框交互时根本不会被调用(甚至从Activity
,您在其中监视交互).
As far as I know, the onUserInteraction()
is simply not called while the user is interacting with a dialog (even started from Activity
in which you're monitoring interactions).
我知道的两个解决方案是:
Two solutions I know are:
子类
Dialog
/DialogPreference
类并覆盖dispatchTouchEvent()
.
实现Window.Callback
接口并将其设置为Dialog
的窗口回调,发出:
Implement Window.Callback
interface and set it as Dialog
s window callback by issuing:
dialog.getWindow().setCallback(callbackImplementation);
注意:此实现应通过调用适当的对话框方法来处理所有接收到的事件,或以您自己的方式处理事件(例如通过手动调用 onUserInteraction()
).
Note: this implementation should process all received events by calling appropriate dialog methods or handle the events in your own way (e.g. by manually calling onUserInteraction()
).
编辑
您有几种方法可以从自定义 PreferenceDialog
实例中获取 Activity
.
You have couple of ways to get Activity
from the custom PreferenceDialog
instance.
调用返回
PreferenceManager
的DialogPreference.getPreferenceManager()
方法.它有一个getActivity()
方法 但它是包私有的 所以你必须把你的自定义DialogPreference
放在android.preference
包来访问它.
Call
DialogPreference.getPreferenceManager()
method which returnsPreferenceManager
. It has agetActivity()
method but it's package-private so you would have to put your customDialogPreference
inandroid.preference
package to access it.
在 PreferenceActivity.onCreate()
中,填充首选项后,使用 findPreference()
找到您的自定义 DialogPreference
钥匙.然后将其转换为您的自定义类并通过访问器将活动设置为 this
.
In the PreferenceActivity.onCreate()
, after inflating the preferences, use findPreference()
to find your custom DialogPreference
by key. Then cast it to your custom class and set activity to this
via an accessor.
我会选择第二个选项.
这篇关于onUserInteraction 在 DialogPreference 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!