2015-10-15 130 views
3

在我的活動中,我得到了一些EditText。當我從主題中移除焦點並使其不可聚焦時,重點將放在另一個EditText上。我以這種方式刪除焦點:當另一個EditText清除焦點時停止EditText獲得焦點

atxtTag.setFocusable(false); 
atxtTag.setFocusableInTouchMode(false); 
atxtTag.clearFocus(); 

我不會將焦點轉移到另一個EditText上。我試圖通過從第一個元素中移除焦點後將焦點設置在另一個視圖上來實現此目的。所以我把重點放在主佈局女巫是一個ScrollView,但它沒有工作。

我該如何實現刪除一個EditText的焦點而不將它轉移到另一個EditText上?

謝謝。

+0

您需要把焦點放在其他可聚焦的小部件上。 ScrollView不可調焦。 – CommonsWare

回答

0

你可以改變清單文件:

android:focusable="false" 
android:focusableInTouchMode="false" 
+0

如果我明白了,我必須在xml中的每個EditText上放置android:focusable =「false」android:focusableInTouchMode =「false」。是對的嗎? –

+0

正確,應該防止EditText被聚焦 –

3

clearFocus方法implementation是否clearFocusInternal呼叫傳遞truerefocus參數的值。這導致了第一個可聚焦元素的請求重點。所以,一個可能的解決方案可能是通過反射來撥打clearFocusInternal(true, false);,但我沒有真正檢查。

挖進requestFocusimplementation我發現它檢查了根視圖的DescendantFocusability。使用我已經建立了一個有效的解決方案:

ViewGroup rootView = (ViewGroup) atxtTag.getRootView(); 
int dfValue = rootView.getDescendantFocusability(); 
rootView.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); 
atxtTag.clearFocus(); 
rootView.setDescendantFocusability(dfValue); 

此外,重點清理不隱藏軟鍵盤,所以你可能需要隱藏它,以及:

InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
inputMethodManager.hideSoftInputFromWindow(atxtTag.getWindowToken(), 0);