簡體   English   中英

當BottomSheet打開時顯示鍵盤

[英]Show keyboard when BottomSheet opens

我希望在我的應用程序中打開某些片段時自動出現鍵盤。 為此,我創建了一個擴展函數showKeyboard()

fun EditText.showKeyboard() {
    this.requestFocus()
    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
    imm?.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}

這個函數在fragment中工作得很好,但由於某種原因,它在BottomSheets中不起作用。

這是我如何在片段中使用它(這有效 ✅ ):

override fun onResume() {
    super.onResume()
    binding.nickEdit.showKeyboard()
}

這是我在BottomSheet中使用它的方法(這不起作用❌):

override fun onResume() {
    super.onResume()
    binding.searchEdit.showKeyboard()
}

我已經嘗試將showkeyboard()函數添加到onViewCreated() ,但是當 BottomSheet 打開時鍵盤仍然沒有出現。 我怎樣才能解決這個問題?

我通過將windowSoftInputMode屬性添加到我的 BottomSheet 樣式並將值設置為adjustResize解決了這個問題。 這是我的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM