简体   繁体   English

如果键盘可见,则防止关闭 BottomSheetDialogFragment

[英]Prevent dismissal of BottomSheetDialogFragment if keyboard is visible

I have a use case where a BottomSheetDialogFragment is having an edit text and this edit text is in focus.我有一个用例,其中 BottomSheetDialogFragment 有一个编辑文本,并且这个编辑文本是焦点。 And keyboard is open on top of BottomSheetDialogFragment.并且键盘在 BottomSheetDialogFragment 的顶部打开。 The requirement is when a user clicks outside of dialog, first keyboard should be dismissed without dismissing the dialog.要求是当用户在对话框之外单击时,应关闭第一个键盘而不关闭对话框。 This gives the user a chance to re-click on edit text and keyboard reappears.这使用户有机会重新单击编辑文本并重新出现键盘。 And once keyboard is in hidden state, then if user clicks outside of dialog, then dialog is dismissed.一旦键盘处于隐藏 state 中,则如果用户在对话框之外单击,则对话框将被关闭。 But this is not happening when user clicks outside of dialog and keyboard is visible then dialog is dismissed.但是当用户在对话框之外单击并且键盘可见然后对话框被关闭时,这不会发生。 How can i intercept the touch events to alter this behaviour?我怎样才能拦截触摸事件来改变这种行为?

This is a tricky one.这是一个棘手的问题。 Something you could do, is to add a listener for whenever the keyboard is visible like they do here , and make your BottomSheetDialogFragment cancellable whenever it is not showing, and non-cancellable whenever it is by callling您可以做的事情是在BottomSheetDialogFragment可见时添加一个侦听器,就像在这里一样

bottomSheetDialog.isCancellable = true/false

I can think of two ways to achieve this. 我可以想到两种方法来实现这一点。
First is to override `BottomSheetDialogFragment#onCreateDialog` method and provide a custom dialog like this 首先是重写 `BottomSheetDialogFragment#onCreateDialog` 方法并提供这样的自定义对话框
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val dialog = object: BottomSheetDialog(requireContext()) { override fun onTouchEvent(event: MotionEvent): Boolean { /* detect touch outside here and hide keyboard */ return super.onTouchEvent(event) } } return dialog.apply { setCanceledOnTouchOutside(false) setContentView(R.layout.bottom_sheet_layout) } }

Second one is creating your own bottom sheet dialog fragment by using a full screen DialogFragment , CoordiatorLayout and BottomSheetBehavior and detect whenever root container was clicked and do things based on your requirements.第二个是通过使用全屏DialogFragmentCoordiatorLayoutBottomSheetBehavior创建您自己的底部工作表对话框片段,并检测何时单击根容器并根据您的要求执行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM