問(wèn)題描述
預(yù)計(jì) onUserInteraction
將被調(diào)用以進(jìn)行任何用戶交互.它在 PreferenceActivity
中運(yùn)行良好.但是,當(dāng)彈出 DialogPreference
時(shí),即使有觸摸事件等用戶交互,也不會(huì)再調(diào)用 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
似乎不是唯一的情況.每當(dāng)顯示 Dialog
時(shí),它都不會(huì)將用戶交互報(bào)告給活動(dòng).
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.
推薦答案
據(jù)我所知,onUserInteraction()
在用戶與對(duì)話框交互時(shí)根本不會(huì)被調(diào)用(甚至從Activity
,您在其中監(jiān)視交互).
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).
我知道的兩個(gè)解決方案是:
Two solutions I know are:
子類(lèi)
Dialog
/DialogPreference
類(lèi)并覆蓋dispatchTouchEvent()
.
實(shí)現(xiàn)Window.Callback
接口并將其設(shè)置為Dialog
的窗口回調(diào),發(fā)出:
Implement Window.Callback
interface and set it as Dialog
s window callback by issuing:
dialog.getWindow().setCallback(callbackImplementation);
注意:此實(shí)現(xiàn)應(yīng)通過(guò)調(diào)用適當(dāng)?shù)膶?duì)話框方法來(lái)處理所有接收到的事件,或以您自己的方式處理事件(例如通過(guò)手動(dòng)調(diào)用 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
實(shí)例中獲取 Activity
.
You have couple of ways to get Activity
from the custom PreferenceDialog
instance.
調(diào)用返回
PreferenceManager
的DialogPreference.getPreferenceManager()
方法.它有一個(gè)getActivity()
方法 但它是包私有的 所以你必須把你的自定義DialogPreference
放在android.preference
包來(lái)訪問(wèn)它.
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()
中,填充首選項(xiàng)后,使用 findPreference()
找到您的自定義 DialogPreference
鑰匙.然后將其轉(zhuǎn)換為您的自定義類(lèi)并通過(guò)訪問(wèn)器將活動(dòng)設(shè)置為 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.
我會(huì)選擇第二個(gè)選項(xiàng).
這篇關(guān)于onUserInteraction 在 DialogPreference 中不起作用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!