现在我找到了这个奇怪问题的解决方案,我将其发布以帮助遇到相同问题的任何人或找到更好的建议:)
我的问题与片段有关。
我有一个包含 2 个片段的活动。加载活动时,它会使用替换事务打开第一个片段
mTransaction.replace(R.id...
第一个片段有 2 个 EditText。当用户单击第一个片段中的按钮时,活动将打开第二个片段,其中包含发生错误的 5 个 EditTexts。这就是我进行第二次片段交易的方式:
mTransaction.add(R.id...
mTransaction.addToBackStack(null);
mTransaction.commit();
这就是问题所在,因为当提交第二个片段时,我使用add 进行片段事务,这使得第一个片段留在容器中,所以这导致了我的错误。
在我看来,第一个片段的 EditText 在那里并且获得了焦点,即使在第二个片段中我使用了android:nextFocusDown="@+id/..."(因为我知道 EditText 4-th 和 5-th 有问题)它没有没用。
解决方案
在我的情况下,解决方案非常简单,我必须像这样进行第二个片段事务:
mTransaction.replace(R.id...
mTransaction.addToBackStack(null);
mTransaction.commit();
如果有人有更好的建议欢迎
第二个解决方案(2018 年):
为此question
我解决了这个问题,为片段布局的父 ViewGroup 创建了一个自定义类。在您的自定义 ViewGroup 中,您可以覆盖该方法
focusSearch(focused: View?, direction: Int)
添加此逻辑:
override fun focusSearch(focused: View?, direction: Int): View? {
val focusSearch = super.focusSearch(focused, direction)
if (direction == View.FOCUS_DOWN) when (focused?.id) {
R.id.some_view_that_has_focus -> return new_view_focus
}
if (findViewById<View>(focusSearch.id) == null) {//the view found is not part of this parent return null
return null
}
return focusSearch
}
当新的焦点视图不属于 parent 时,返回 null。当某些其他视图焦点无法访问时,我会在 when 案例中手动管理它。