我试图在键盘打开时隐藏布局底部的一个特定按钮,以便为用户提供更多视图。
随着发布androidx.core:core-ktx:1.5.0-alpha02
谷歌(最后)添加了一个名为的方法insets.isVisible(WindowInsetsCompat.Type.ime())
无论键盘是打开还是单击,它都会返回一个布尔值。
我正在使用基类EmailFragment
这里我设置了函数,以实现上面写的功能。我的问题是我的ViewCompat.setOnApplyWindowInsetsListener(view)
永远不会被调用(没有吐司等)。
我也尝试过设置ViewCompat.setOnApplyWindowInsetsListener(view)
直接在使用过的片段中,但这没有改变任何东西。
我的最小 API 是 21,在我的 AndroidManifest.XML 中我有android:windowSoftInputMode = adjustResize
Code
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 的 Fragment(五分之一)
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
让我的按钮“不可见”,但我不能再滚动了,很伤心..
另一个解决方案可能是键盘只是与按钮重叠,但我不知道该怎么做......
我感谢每一个帮助,谢谢。