我正在使用 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'
有什么建议么?