如何在Android上清除焦点和删除键盘?

如何在Android上清除焦点和删除键盘?,android,Android,我有一个编辑文本控件。 如果我点击它,当我按下“enter/ok/return”时,软键盘将弹出,然后编辑文本控件仍有焦点,键盘向上。 如何关闭软键盘并从中移除焦点?您可以尝试在布局中的另一个元素上执行SetFocus() 如果您正在谈论键盘上的“enter/ok/return”按钮,您可能需要在EditText控件上设置KeyListener,以便知道何时在另一个元素上设置SetFocus()。在布局XML文件中,在EditText上指定一个选项: android:imeOptions="ac

我有一个编辑文本控件。 如果我点击它,当我按下“enter/ok/return”时,软键盘将弹出,然后编辑文本控件仍有焦点,键盘向上。

如何关闭软键盘并从中移除焦点?

您可以尝试在布局中的另一个元素上执行
SetFocus()


如果您正在谈论键盘上的“enter/ok/return”按钮,您可能需要在
EditText
控件上设置
KeyListener
,以便知道何时在另一个元素上设置
SetFocus()

在布局XML文件中,在EditText上指定一个选项:

android:imeOptions="actionGo"
接下来,将操作侦听器添加到活动的java文件中的EditText中

    mYourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_GO) {
                // hide virtual keyboard
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(mYourEditText.getWindowToken(), 0);
                return true;
            }
            return false;
        }
    });

其中mYourEditText是EditText对象

请确保您的EditText XML具有:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editTextField.getWindowToken(), 0);
android:id="@+id/myEditText"    
android:imeOptions="actionDone"
然后将listener设置为EditText(使用Kotlin并从片段中):


适用于我的Kotlin解决方案,移除所有活动焦点并关闭软键盘。 在父布局上设置android:focusableInTouchMode=“true”。 在我的例子中,我也有一个滚动视图。我正在触摸事件侦听器中设置view.clearFocus()。这将清除任何触摸文本视图上的所有焦点。然后我继续关闭屏幕上的软键盘

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
    ...
    android:focusableInTouchMode="true" //<--- this is important

    tools:context=".SomFragment">

    <ScrollView
        android:id="@+id/scroll_view_container"
        ...
        >


        <TextView
            android:id="@+id/sone_text_1"
            ...
             />

        <TextView
            android:id="@+id/sone_text_2"
            ...
             />

嗯,谢谢。但是我被允许在版面或编辑文本中写imeOpt?我在EditorInfo上出错了,知道吗?你能解释一下EditorInfo吗?这会关闭键盘,但不会在所有情况下移除焦点。IE android将请求聚焦在第一个可以聚焦的可用视图上。这只会隐藏键盘。它不会移除焦点。我知道这是许多人给出的解决方案,但我不喜欢它,因为它不是简单地工作的输入。clearFocus()即从该输入中清除焦点,而是现在必须将焦点设置为其他内容!?这似乎是违反直觉的。
myEditText.setOnEditorActionListener({ v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                myEditText.clearFocus()
                val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                imm.hideSoftInputFromWindow(view!!.windowToken, 0)    
            }
            false
        })
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
    ...
    android:focusableInTouchMode="true" //<--- this is important

    tools:context=".SomFragment">

    <ScrollView
        android:id="@+id/scroll_view_container"
        ...
        >


        <TextView
            android:id="@+id/sone_text_1"
            ...
             />

        <TextView
            android:id="@+id/sone_text_2"
            ...
             />
scroll_view_container.setOnTouchListener { v, event ->
        view.clearFocus()
        hideSoftKeyboard()
        true
    }

private fun hideSoftKeyboard() {
    val windowToken = view?.rootView?.windowToken
    windowToken?.let{
        val imm = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(it, 0)
    }
}