0

我正在使用 WindowInsets API 来打开/隐藏键盘,但它似乎不适用于 Compat 版本(目标 API 29)或其他东西。

fun View.hideKeyboard() {
    ViewCompat.getWindowInsetsController(this)?.hide(WindowInsetsCompat.Type.ime())
}

fun View.showKeyboard() {
    ViewCompat.getWindowInsetsController(this)?.show(WindowInsetsCompat.Type.ime())
}

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<EditText>(R.id.text).showKeyboard()
    }
}

布局:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
implementation 'androidx.core:core-ktx:1.6.0-alpha01'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

有什么建议么?

4

1 回答 1

1

参考WindowInsetsControllerCompat source,这里view.hasWindowFocus()返回 false 因为窗口还没有获得焦点。如果将键盘交互设置为按钮,则窗口有时间获得焦点,一切正常。

这里有两种方法:

  1. 等待焦点更改为truewith viewTreeObserver.addOnWindowFocusChangeListener
    editText.viewTreeObserver.addOnWindowFocusChangeListener(
                object : ViewTreeObserver.OnWindowFocusChangeListener {
                    override fun onWindowFocusChanged(hasFocus: Boolean) {
                        if (hasFocus) {
                            editText.showKeyboard()
                            // show only once
                            editText.viewTreeObserver.removeOnWindowFocusChangeListener(this)
                        }
                    }
                })
    
  2. 当窗口获得焦点时使键盘可见:
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
    editText.showKeyboard()
    
于 2021-04-14T05:04:10.147 回答