如何使用 WindowInsets API(Compat 版本)到 show/hide 键盘?
How to use WindowInsets API (Compat version) to show/hide keyboard?
我正在使用 WindowInsets API 到 opening/hiding 键盘,但它似乎不适用于 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'
有什么建议吗?
参考 WindowInsetsControllerCompat source,这里 view.hasWindowFocus()
returns false 因为 window 还没有获得焦点。如果将键盘交互设置为按钮,那么 window 有时间获得焦点并且一切正常。
这里有两种方法:
- 等待焦点更改为
true
和 viewTreeObserver.addOnWindowFocusChangeListener
:
editText.viewTreeObserver.addOnWindowFocusChangeListener(
object : ViewTreeObserver.OnWindowFocusChangeListener {
override fun onWindowFocusChanged(hasFocus: Boolean) {
if (hasFocus) {
editText.showKeyboard()
// show only once
editText.viewTreeObserver.removeOnWindowFocusChangeListener(this)
}
}
})
- 当 window 获得焦点时使键盘可见:
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
editText.showKeyboard()
我正在使用 WindowInsets API 到 opening/hiding 键盘,但它似乎不适用于 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'
有什么建议吗?
参考 WindowInsetsControllerCompat source,这里 view.hasWindowFocus()
returns false 因为 window 还没有获得焦点。如果将键盘交互设置为按钮,那么 window 有时间获得焦点并且一切正常。
这里有两种方法:
- 等待焦点更改为
true
和viewTreeObserver.addOnWindowFocusChangeListener
:editText.viewTreeObserver.addOnWindowFocusChangeListener( object : ViewTreeObserver.OnWindowFocusChangeListener { override fun onWindowFocusChanged(hasFocus: Boolean) { if (hasFocus) { editText.showKeyboard() // show only once editText.viewTreeObserver.removeOnWindowFocusChangeListener(this) } } })
- 当 window 获得焦点时使键盘可见:
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) editText.showKeyboard()