【问题标题】:android EditText focus behavior [duplicate]android EditText焦点行为[重复]
【发布时间】:2021-05-03 04:02:49
【问题描述】:

我认为这将是一个简单的实现,但显然不是。我有一个带有 2 个 editTexts 和 1 个 TextView 的片段。都有用于背景的自定义 Drawables。这是我正在寻找的行为:

  1. 片段打开时,没有焦点 - 正在工作
  2. 当您点击任一编辑时,它会获得焦点,所有文本都被选中,并且 softKeyboard 以数字模式打开 - 这也有效
  3. 输入新号码后,当点击软键盘右下角的蓝色键(制表符或复选标记符号)时,软键盘关闭,没有任何焦点 - 没有闪烁的光标。 - 这就是我不能去工作的原因
    请帮忙。 Kotlin 或 Java 都可以

【问题讨论】:

    标签: android android-edittext focus


    【解决方案1】:

    你需要实现这个逻辑(如果你愿意,你可以进一步升级它,这只是一个例子)

    布局:

    ...
    
            <View
                android:id="@+id/f_c_focus_view"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:layout_width="0dp"
                android:layout_height="0dp" />
    
            <EditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:imeOptions="actionDone"
                android:inputType="number" />
    
            <EditText
                android:id="@+id/editText2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:imeOptions="actionDone"
                android:inputType="number"/>
    ...
    

    在布局中您可以看到隐藏的“焦点”视图,这是拦截焦点所必需的,因为如果您将 EditText 作为视图的第一个组件,它总是会获得焦点。我的 EditText 组件有一个 imeOptions="actionDone",我将在代码中进一步处理,你可以有不同的 imeOptions。

    代码:

        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
    
        ...
    
            focusView.requestFocus() // intercept focus on view created
    
            editText1.setOnEditorActionListener { v, actionId, _ ->
                when(actionId) {
                    EditorInfo.IME_ACTION_DONE -> { // handle checkmark key press on the keyboard
                        v.clearFocus()
                        hideKeyBoard(v)
                    }
                }
    
                false
            }
    
            editText2.setOnEditorActionListener { v, actionId, _ ->
                when(actionId) {
                    EditorInfo.IME_ACTION_DONE -> {
                        v.clearFocus()
                        hideKeyBoard(v)
                    }
                }
    
                false
            }
    
       ...
    }
    
    private fun hideKeyBoard(v: View) {
        val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(v.windowToken, 0)
    }
    

    【讨论】:

    • 谢谢你,阿尔乔姆。小睡片刻后,我想出了一个与您的解决方案有些相似但没有额外视图的解决方案。我决定不再担心焦点,而是担心光标。我使用 OnEditorActionListener 将光标隐藏在 IME_ACTION_DONE 上。我使用 OnClick 再次显示它。当用户连续两次进入相同的编辑时,我必须将 selectAll 添加到 onClick 以处理(因为技术上的焦点没有改变)
    猜你喜欢
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 2015-01-10
    • 2017-12-23
    • 2011-03-31
    相关资源
    最近更新 更多