問題描述
我有一個包含兩個選項卡的非常簡單的活動,我正在嘗試在自定義視圖中處理鍵盤輸入.這很好用......直到我交換標簽.一旦我交換標簽,我就再也無法捕捉到這些事件了.然而,在另一個應用程序中,打開一個對話框然后關閉它會允許我的關鍵事件通過.如果不這樣做,我將無法再次獲取我的關鍵事件.
I have a very simple activity with two tabs, and I'm trying to handle keyboard input in a custom view. This works great... until I swap tabs. Once I swap tabs, I can never get the events to capture again. In another application, opening a Dialog and then closing it, however, would allow my key events to go through. Without doing that I've found no way of getting my key events again.
這里有什么問題?一旦我交換標簽,我就找不到任何方法來獲取關鍵事件,并且很好奇是什么在吃它們.這個例子很簡短.
What's the issue here? I can't find any way to get key events once I swap tabs, and am curious what's eating them. This example is pretty short and to the point.
my_fragment.xml
KeyboardTestActivity.java
推薦答案
我已經解決了自己的問題,所以我想我會分享解決方案.如果有一些措辭問題,請在評論中糾正我;我盡量做到準確,但我并不完全是安卓專家.這個答案也應該作為一個很好的例子來說明如何處理一般的換出 ActionBar 選項卡.不管你是否喜歡解決方案代碼的設計,它應該是有用的.
I've solved my own problem, so I thought I'd share the solution. If there's some wording issue, please correct me in a comment; I'm trying to be as accurate as I can but I'm not entirely an android expert. This answer should also serve as an excellent example of how to handle swapping out ActionBar tabs in general. Whether or not one likes the design of the solution code, it should be useful.
以下鏈接幫助我找出了我的問題:http://code.google.com/p/android/issues/detail?id=2705
The following link helped me figure out my issue: http://code.google.com/p/android/issues/detail?id=2705
事實證明,手頭有兩個重要問題.首先,如果一個 View 既是 android:focusable 又是 android:focusableInTouchMode,那么在蜂窩平板電腦上,人們可能會期望點擊它和類似的東西會聚焦它.然而,這不一定是真的.如果該視圖恰好也是 android:clickable,那么確實點擊會聚焦該視圖.如果它不可點擊,它不會被觸摸聚焦.
It turns out, there are two important issues at hand. Firstly, if a View is both android:focusable and android:focusableInTouchMode, then on a honeycomb tablet one might expect that tapping it and similar would focus it. This, however, is not necessarily true. If that View happens to also be android:clickable, then indeed tapping will focus the view. If it is not clickable, it will not be focused by touch.
此外,當換出片段時,會出現與第一次為活動實例化視圖時非常相似的問題.只有在完全準備好視圖層次結構后,才需要進行某些更改.
Furthermore, when swapping out a fragment there's an issue very similar to when first instantiating the view for an activity. Certain changes need to be made only after the View hierarchy is completely prepared.
如果在視圖層次結構完全準備好之前對片段內的視圖調用requestFocus()",則視圖確實會認為它已聚焦;但是,如果軟鍵盤啟動,它實際上不會向該視圖發送任何事件!更糟糕的是,如果那個 View 是可點擊的,此時點擊它并不能解決這個鍵盤焦點問題,因為 View 認為它確實是焦點,沒有什么可做的.但是,如果要聚焦某個其他視圖,然后再點擊該視圖,因為它既可點擊又可聚焦,它確實會聚焦并將鍵盤輸入定向到該視圖.
If you call "requestFocus()" on a view within a fragment before the View hierarchy is completely prepared, the View will indeed think that it is focused; however, if the soft keyboard is up, it will not actually send any events to that view! Even worse, if that View is clickable, tapping it at this point will not fix this keyboard focus issue, as the View thinks that it is indeed focused and there is nothing to do. If one was to focus some other view, and then tap back onto this one, however, as it is both clickable and focusable it would indeed focus and also direct keyboard input to this view.
鑒于該信息,在切換到選項卡時設置焦點的正確方法是在切換到片段后將可運行對象發布到片段的視圖層次結構,然后才調用 requestFocus().在視圖層次結構完全準備好之后調用 requestFocus() 將聚焦視圖以及直接鍵盤輸入到它,如我們所愿.它不會進入那種奇怪的焦點狀態,即視圖聚焦但鍵盤輸入不知何故沒有指向它,如果在視圖層次結構完全準備好之前調用 requestFocus() 就會發生這種情況.
Given that information, the correct approach to setting the focus upon swapping to a tab is to post a runnable to the View hierarchy for the fragment after it is swapped in, and only then call requestFocus(). Calling requestFocus() after the View hierarchy is fully prepared will both focus the View as well as direct keyboard input to it, as we want. It will not get into that strange focused state where the view is focused but the keyboard input is somehow not directed to it, as will happen if calling requestFocus() prior to the View hierarchy being fully prepared.
同樣重要的是,在片段布局的 XML 中使用requestFocus"標簽會過早調用 requestFocus().沒有理由在片段的布局中使用該標簽.在片段之外,也許.. 但不在片段之內.
Also important, using the "requestFocus" tag within the XML of a fragment's layout will most call requestFocus() too early. There is no reason to ever use that tag in a fragment's layout. Outside of a fragment, maybe.. but not within.
在代碼中,我在片段頂部添加了一個 EditText,僅用于測試點擊焦點更改行為,點擊自定義視圖也會切換軟鍵盤.交換選項卡時,焦點也應默認為自定義視圖.我試圖有效地注釋代碼.
In the code, I've added an EditText to the top of the fragment just for testing tap focus change behaviors, and tapping the custom View will also toggle the soft keyboard. When swapping tabs, the focus should also default to the custom view. I tried to comment the code effectively.
main.xml
my_fragment.xml
這篇關于Android 操作欄選項卡和鍵盤焦點的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!