Android 如何获取隐藏键盘事件

Android 如何获取隐藏键盘事件,android,Android,我想知道如何从我的软键盘中获取隐藏事件,当我单击我的编辑文本(左侧的第一个按钮)时显示该事件: 但是有了这个覆盖,我不能得到隐藏事件,只能得到back事件。有人知道如何获取该事件吗?当您单击任何接收用户输入的字段时,键盘会出现。默认情况下,该键盘由安卓操作系统提供。 要自己管理键盘,可以使用底部工作表。为此,您必须制作一个自定义键盘,并手动获取每个输入。之后,您可以执行onbackpressed方法,直接返回。在这段代码中,我正在听editText。如果用户关闭键盘,OnBackpress()

我想知道如何从我的软键盘中获取隐藏事件,当我单击我的编辑文本(左侧的第一个按钮)时显示该事件:


但是有了这个覆盖,我不能得到隐藏事件,只能得到back事件。有人知道如何获取该事件吗?

当您单击任何接收用户输入的字段时,键盘会出现。默认情况下,该键盘由安卓操作系统提供。
要自己管理键盘,可以使用底部工作表。为此,您必须制作一个自定义键盘,并手动获取每个输入。之后,您可以执行onbackpressed方法,直接返回。

在这段代码中,我正在听editText。如果用户关闭键盘,OnBackpress()将运行


尝试将焦点更改侦听器附加到edittext?你是什么意思?当我第一次单击back(并且键盘隐藏)时,焦点保持在我的EditText上,因此没有焦点更改,对吗?好的,谢谢,自定义键盘的问题是用户必须在Android设置中的可用键盘中手动添加它。或者有没有办法强迫它取代Android的默认版本?
<EditText
        android:id="@+id/txtEdit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="text|textAutoComplete"
        android:windowSoftInputMode="stateHidden|adjustResize"/>
@Override
public void onBackPressed() {
    // my code
}
 var isFirst: Boolean = true

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    editText.getViewTreeObserver().addOnGlobalLayoutListener(ViewTreeObserver.OnGlobalLayoutListener {
        if (isKeyboardOpen()) {
            if (isFirst) {
                 isFirst = false
            }
        } else {
            if (!isFirst) {
                onBackPressed()
            }
        }
    })

}

private fun isKeyboardOpen(): Boolean {
    val r = Rect()
    editText.getWindowVisibleDisplayFrame(r)
    val screenHeight = editText.getRootView().getHeight()
    // r.bottom is the position above soft keypad or device button.
    // if keypad is shown, the r.bottom is smaller than that before.
    val keypadHeight = screenHeight - r.bottom
    // Log.d("TAG", "keypadHeight = $keypadHeight")

    if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
        // keyboard is opened
        Log.d("TAG", "keypad is open")
    } else {
        // keyboard is closed
        Log.d("TAG", "keypad is close")
    }
    return (keypadHeight > screenHeight * 0.15)
}

override fun onBackPressed() {
    // my code
    Log.d("TAG", "keypad is onBackPressed")
    finish()
}