当键盘打开时,我试图在布局底部隐藏一个特定按钮,以便为用户提供更多 View 。
随着 androidx.core:core-ktx:1.5.0-alpha02
的发布google(终于)添加了一个名为 insets.isVisible(WindowInsetsCompat.Type.ime())
的方法无论键盘是打开还是单击,它都会返回一个 bool 值。
我正在使用基类 EmailFragment
这里我设置函数是为了实现上面写的功能。我的问题是我的 ViewCompat.setOnApplyWindowInsetsListener(view)
永远不会被调用(没有 toast 等)。
我也试过设置ViewCompat.setOnApplyWindowInsetsListener(view)
直接在使用的 fragment 中,但这并没有改变。
我的最小 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 继承的 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
使我的按钮“不可见”,但后来我不能再滚动了,很伤心..另一种解决方案可能是键盘只是与按钮重叠,但我不知道该怎么做......
我感谢每一个帮助,谢谢。
最佳答案
我能够通过以下方式使其工作:
android:windowSoftInputMode="adjustResize"
至Activity
list 中的标签 OnApplyWindowInsetsListener
在 window.decorView
. 但是,这对状态栏产生了不幸的副作用,这意味着 OP 的评论(“我不希望这会奏效”)可能是准确的。希望当它从 alpha 和 beta 毕业时它会起作用。
关于Android:检测打开的键盘,onApplyWindowListener 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63874010/