簡體   English   中英

當另一個EditText清晰焦點時,停止EditText獲得焦點

[英]Stop EditText from gainning focus when another EditText clear focus

在我的活動中,我得到了一些EditText。 當我從一個主題中移除焦點並使其不可聚焦時,焦點將放在另一個EditText上。 我這樣刪除焦點:

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

我不知道將焦點轉移到另一個EditText上。 我嘗試通過從第一個元素中刪除焦點后將焦點設置在另一個視圖上來實現此目的。 所以我把重點放在主要布局上,女巫是ScrollView,但它沒有用。

如何在不將其轉移到另一個EditText上的情況下將焦點移除到一個EditText上?

謝謝。

clearFocus方法的實現做了clearFocusInternal呼叫傳遞true的價值refocus參數。 這導致第一個可聚焦元素的請求焦點。 所以,一個可能的解決方案是調用clearFocusInternal(true, false); 通過反思,但我實際上沒有檢查。

深入研究requestFocus 實現,我發現它檢查了根視圖的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);

您可以更改清單文件:

android:focusable="false"
android:focusableInTouchMode="false"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM