我试图在键盘打开时隐藏布局底部的一个特定按钮,以便让用户获得更多的视图可用性。
随着
我使用一个基类
我还尝试直接在使用的片段中设置
我的最小API为21,在我的AndroidManifest.XML中,我有
随着
androidx.core:core-ktx:1.5.0-alpha02
的发布,Google(终于)添加了一个名为 insets.isVisible(WindowInsetsCompat.Type.ime())
的方法,该方法返回一个布尔值,指示键盘是否已打开或单击。我使用一个基类
EmailFragment
,在其中设置函数以实现上述功能。我的问题是我的 ViewCompat.setOnApplyWindowInsetsListener(view)
从未被调用(没有toast等)。我还尝试直接在使用的片段中设置
ViewCompat.setOnApplyWindowInsetsListener(view)
,但这没有改变任何东西。我的最小API为21,在我的AndroidManifest.XML中,我有
android:windowSoftInputMode = adjustResize
代码:abstract class EmailFragment<out T: ViewDataBinding>(
layout: Int,
// ... some other stuff that is not necessary for the question
) : BaseFragment<T>(layout) {
// ... some other stuff that is not necesarry for the question
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
hideButton(view)
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return super.onCreateView(inflater, container, savedInstanceState)
}
private fun hideButton(view: View) {
ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
val isKeyboardVisible = insets.isVisible(WindowInsetsCompat.Type.ime())
if (isKeyboardVisible) {
btn.visibility = View.GONE
Toast.makeText(requireContext(), "KEYBOARD OPEN", Toast.LENGTH_SHORT).show()
} else {
btn.visibility = View.VISIBLE
Toast.makeText(requireContext(), "KEYBOARD CLOSED", Toast.LENGTH_SHORT).show()
}
// Return the insets to keep going down this event to the view hierarchy
insets
}
}
}
继承自EmailFragment的片段(五个片段之一)
class CalibrateRepairMessageFragment(
//... some other stuff that is not necessary for this question
) : EmailFragment<FragmentCalibrateRepairMessageBinding>(
R.layout.fragment_calibrate_repair_message,
//... some other stuff that is not necessary for this question
) {
//... some other stuff that is not necessary for this question
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//... some other stuff that is not necessary for this question
}
屏幕截图1(已打码)
屏幕截图2,无法工作(已打码)
我知道使用 android:windowSoftInputMode = adjustPen
会使我的按钮“不可见”,但这样我就不能再滚动了,很遗憾..
另一个解决方案可能是让键盘覆盖在按钮上,但我不知道如何做到...
非常感谢您的帮助,谢谢。