[英]Show keyboard when BottomSheet opens
I want the keyboard to appear automatically when certain fragments are open in my application.我希望在我的应用程序中打开某些片段时自动出现键盘。 To this end, I created an extension function
showKeyboard()
:为此,我创建了一个扩展函数
showKeyboard()
:
fun EditText.showKeyboard() {
this.requestFocus()
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
imm?.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
This function works very well in fragments, but for some reason, it doesn't work in BottomSheets.这个函数在fragment中工作得很好,但由于某种原因,它在BottomSheets中不起作用。
Here's how I use it in fragments (this works ✅ ):这是我如何在片段中使用它(这有效 ✅ ):
override fun onResume() {
super.onResume()
binding.nickEdit.showKeyboard()
}
Here's how I use it in BottomSheet (this doesn't work ❌):这是我在BottomSheet中使用它的方法(这不起作用❌):
override fun onResume() {
super.onResume()
binding.searchEdit.showKeyboard()
}
I have tried adding showkeyboard()
function to onViewCreated()
, but the keyboard still doesn't appear when the BottomSheet opens.我已经尝试将
showkeyboard()
函数添加到onViewCreated()
,但是当 BottomSheet 打开时键盘仍然没有出现。 How can I fix this?我怎样才能解决这个问题?
I solved this issue by adding windowSoftInputMode
attribute to my BottomSheet style and setting the value to adjustResize
.我通过将
windowSoftInputMode
属性添加到我的 BottomSheet 样式并将值设置为adjustResize
解决了这个问题。 Here's the full version of my BottomSheet style:这是我的BottomSheet样式的完整版本:
<style name="MyBottomSheetDialogTheme" parent="Theme.Design.Light.BottomSheetDialog">
<item name="bottomSheetStyle">@style/MyBottomsheetStyle</item>
<item name="colorPrimary">@color/white</item>
<item name="colorAccent">@color/white</item>
<item name="colorControlNormal">@color/white</item>
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustResize</item>
</style>
<style name="MyBottomsheetStyle" parent="Widget.Design.BottomSheet.Modal">
<item name="android:background">@drawable/background_bottom_sheet</item>
</style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.