問題描述
預計 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 中不起作用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!