在BottomSheetDialogFragment关闭的时候收起软键盘
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
setStyle(STYLE_NO_TITLE, R.style.BottomDialogSheet)
return object : BottomSheetDialog(requireContext(),theme) {
override fun dismiss() {
KeyboardUtils.hideSoftInput(requireView())
super.dismiss()
}
}
}
使用步骤
1.导入AirPanel
implementation 'net.qiujuer.widget:airpanel:1.5.0'
2.设置windowSoftInputMode
<style name="BottomDialog">
<!--status的颜色-->
<item name="android:statusBarColor">#7A7A7A</item>
<!--背景透明-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--dialog外面的黑色透明度-->
<item name="android:backgroundDimEnabled">true</item>
<item name="android:backgroundDimAmount">0.5</item>
<!--是否允许点击屏幕外关闭-->
<item name="android:windowCloseOnTouchOutside">true</item>
<!--配合AirPanel-->
<item name="android:windowSoftInputMode">adjustResize</item>
<!--动画设置-->
<!-- <item name="android:windowAnimationStyle">@style/Animation.Design.BottomSheetDialog</item>-->
</style>
3.布局界面
- 需要有两个AirPanelView嵌套
- airPanelMainLayout
主AirPanel用于监听软键盘的变化 - airPanelSubLayout
- 用于站位,大小自动根据软键盘的高度而变化
- 默认状态设置为gone不显示android:visibility=“gone”,监听到键盘变化后,会自动做出改变,改变大小和显示状态
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
</data>
<net.qiujuer.widget.airpanel.AirPanelLinearLayout
android:id="@+id/airPanelMainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="40dp" />
<Button
android:id="@+id/bt_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
<TextView
android:id="@+id/bt_other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_margin="40dp"
android:gravity="center"
android:text="其他登录方式" />
<net.qiujuer.widget.airpanel.AirPanelFrameLayout
android:id="@+id/airPanelSubLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:visibility="gone" />
</net.qiujuer.widget.airpanel.AirPanelLinearLayout>
</layout>
4.View中设置监听
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = object : BottomSheetDialog(requireContext(), theme) {
override fun dismiss() {
KeyboardUtils.hideSoftInput(requireView())
super.dismiss()
}
}
val window: Window? = dialog.window
window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
window?.setDimAmount(0f)
return dialog
}
private const val TAG = "BottomFragment"
class BottomFragment : BottomSheetDialogFragment() {
lateinit var binding: FragmentBottomBinding
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
setStyle(STYLE_NO_TITLE, R.style.BottomDialog)
return object : BottomSheetDialog(requireContext(), theme) {
override fun dismiss() {
Util.hideKeyboard(requireView())
super.dismiss()
}
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_bottom, container, false)
binding.airPanelMainLayout.setOnStateChangedListener(object :
AirPanel.OnStateChangedListener {
override fun onPanelStateChanged(isOpen: Boolean) {
Log.e(TAG, "onPanelStateChanged: $isOpen")
}
override fun onSoftKeyboardStateChanged(isOpen: Boolean) {
Log.d(TAG, "onSoftKeyboardStateChanged: $isOpen")
binding.btOther.visibility = if (isOpen) View.GONE else View.VISIBLE
}
})
return binding.root
}
companion object {
@JvmStatic
fun newInstance() = BottomFragment()
}
}
参考资料