Android 软件盘相关的问题(在BottomSheetDialogFragment中无法隐藏问题、使用AirPanel适配软键盘高度问题)

在这里插入图片描述

在BottomSheetDialogFragment关闭的时候收起软键盘

  • 在创建Dialog的时候,重写dismiss方法
    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

	//https://github.com/qiujuer/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
    1. 用于站位,大小自动根据软键盘的高度而变化
    2. 默认状态设置为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>

    <!--主AirPanel,id可以自由设置-->
    <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="其他登录方式" />

        <!--子AirPanel,id需要设置为airPanelSubLayout-->
        <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中设置监听

  • BottomFragment
    用于登录的弹窗
    //防止软键盘无法关闭
    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))
        //设置dialog周围activity背景的透明度,[0f,1f],0全透明,1不透明黑
        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()
    }
}

参考资料

猜你喜欢

转载自blog.csdn.net/yu540135101/article/details/116763291