如何检测软件键盘在Android设备上是否可见?Android中是否有一种检测软件(即.a)的方法。(“软”)键盘在屏幕上可见吗?
3 回答
繁星coding
TA贡献1797条经验 获得超4个赞
windowSoftInputMode
adjustNothing
.
boolean isKeyboardShowing = false;void onKeyboardVisibilityChanged(boolean opened) { print("keyboard " + opened);}// ContentView is the root view of the layout of this activity/fragment contentView.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Rect r = new Rect(); contentView.getWindowVisibleDisplayFrame(r); int screenHeight = contentView.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. int 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 if (!isKeyboardShowing) { isKeyboardShowing = true onKeyboardVisibilityChanged(true) } } else { // keyboard is closed if (isKeyboardShowing) { isKeyboardShowing = false onKeyboardVisibilityChanged(false) } } }});
繁华开满天机
TA贡献1816条经验 获得超4个赞
InputMethodManager imm = (InputMethodManager) getActivity() .getSystemService(Context.INPUT_METHOD_SERVICE); if (imm.isAcceptingText()) { writeToLog("Software Keyboard was shown"); } else { writeToLog("Software Keyboard was not shown");
- 3 回答
- 0 关注
- 402 浏览
添加回答
举报
0/150
提交
取消