记一下,以后不用找在InputMethodService中有这两个方法可以看到软键盘显示状态
//软键盘隐藏 override fun onWindowHidden() {super.onWindowHidden() }
//软键盘显示 override fun onWindowShown() {super.onWindowShown() }
在activity中可以通过这种方法看到软键盘显示状态
view.viewTreeObserver.addOnGlobalLayoutListener {// 获取屏幕的高度val screenHeight: Int = view.getRootView().getHeight()// 获取当前布局的可见高度val rect = Rect()view.getWindowVisibleDisplayFrame(rect)// 计算键盘的高度var keyboardHeight = screenHeight - rect.bottom;if (keyboardHeight > screenHeight * 0.15) {// 键盘显示showKeyBoardHeight = keyboardHeightif (showKeyBoardHeight - hideKeyBoardHeight > 0) {}} else {hideKeyBoardHeight = keyboardHeight// 键盘隐藏} }