【问题标题】:EditText actionNext doesn't work inside fragmentsEditText actionNext 在片段中不起作用
【发布时间】:2015-07-07 00:07:20
【问题描述】:

我有一个包含5 EditTexts 的片段,前四个有actionNext,最后一个有actionDone。我有一个问题,当我在4-th EditText 并在软键盘上按 next 时,它会从第 4 个 EditText 中移除焦点,然后焦点无处可去,键盘仍然可见。当我按下键盘上的下一个按钮时,焦点转到5-th EditText,然后一切都按原样进行。

问题

当我在 4-th EditText 时,我必须在键盘上按 actionNext 2 times 才能到达 5-th EditText。

环境

我正在使用 AppCompatActivity,支持片段,EditText 在 android.support.design.widget.TextInputLayout 内部,我使用 AndroidAnnotations。

【问题讨论】:

    标签: android android-fragments android-edittext focus


    【解决方案1】:

    现在我找到了这个奇怪问题的解决方案,我将其发布以帮助遇到相同问题的任何人或找到更好的建议:)

    我的问题与片段有关。

    我有一个包含 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 案例中手动管理它。

    【讨论】:

      猜你喜欢
      • 2012-09-03
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 2019-07-21
      • 1970-01-01
      • 2023-04-05
      • 2017-07-30
      • 2014-07-08
      相关资源
      最近更新 更多