问题描述
我的 EditText
和 Activity
中有 Button
可以打开 BottomSheet
。但是如果 EditText
有焦点,我需要先隐藏键盘。我正在使用此代码来执行此操作:
fun hideSoftKeyboard(context: Context,view: View? = null) {
try {
val inputMethodManager = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
val focusedView = view ?: (context as? Activity)?.currentFocus
inputMethodManager.hideSoftInputFromWindow(focusedView?.windowToken,0)
} catch (e: NullPointerException) {
LogUtils.w("KeyBoard","NPE in input method service")
}
}
这是我活动中的代码:
class TestActivity : AppCompatActivity() {
private var bsb: BottomSheetBehavior<*>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.test)
bsb = BottomSheetBehavior.from(bottom_sheet)
bsb?.skipCollapsed = true
bsb?.state = BottomSheetBehavior.STATE_HIDDEN
btn_open.setonClickListener {
hideSoftKeyboard(this,et_text)
bsb?.state = BottomSheetBehavior.STATE_EXPANDED
}
}
}
但是我对某些键盘有很大的问题。例如,当我使用三星默认键盘时: https://i.imgur.com/k0DoFQG.mp4
如您所见,键盘没有足够的时间关闭,BottomSheet
已经在其 STATE_EXPANDED
上。
当我切换到 Gboard 时,一切都按预期运行。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)