我在bottomsheetfragment
通过单击后隐藏键盘和键盘时遇到问题。当前的实现要求用户单击两次以关闭键盘和底页。
我实现setOnKeyListener
但第一次后退单击隐藏键盘和第二次单击触发后退事件
对话风格
<style name="BottomSheetDialog" parent="Theme.Design.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustResize</item>
</style>
class CommentInputFragment : BottomSheetDialogFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NORMAL, R.style.BottomSheetDialog);
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
var view= inflater.inflate(R.layout.fragment_comment_input, container, false)
return view;
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
var dialog= super.onCreateDialog(savedInstanceState)
return dialog
}
override fun onResume() {
super.onResume()
dialog?.setOnKeyListener(object: DialogInterface.OnKeyListener
{
override fun onKey(p0: DialogInterface?, keyCode: Int, p2: KeyEvent?): Boolean {
if ((keyCode == android.view.KeyEvent.KEYCODE_BACK))
{
// To dismiss the fragment when the back-button is pressed.
dismiss();
return true;
}
// Otherwise, do nothing else
else return false;
}
})
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
et_add_comment.requestFocus();
}
}