猿问
回到首页
个人中心
反馈问题
注册登录
下载APP
首页
课程
实战
体系课
手记
专栏
慕课教程
如何检测软件键盘在Android设备上是否可见?
如何检测软件键盘在Android设备上是否可见?
Android中是否有一种检测软件(即.a)的方法。(“软”)键盘在屏幕上可见吗?
互换的青春
浏览 403
回答 3
3回答
繁星coding
这对我有用。也许这是最好的方法适用于所有版本.使键盘可见性的属性并观察到这种更改是有效的,因为onGlobalLayout方法多次调用。另外,检查设备的旋转和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) } } }});
0
0
0
繁华开满天机
试试这个:InputMethodManager imm = (InputMethodManager) getActivity() .getSystemService(Context.INPUT_METHOD_SERVICE); if (imm.isAcceptingText()) { writeToLog("Software Keyboard was shown"); } else { writeToLog("Software Keyboard was not shown");
0
0
0
打开App,查看更多内容
随时随地看视频
慕课网APP
相关分类
Android
继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续