中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
向前或向后找到当前拥有焦点的控件,然后对其调用clearFocus()就可以隐藏软键盘。
clearFocus()
逻辑上应该没问题,我也简单测过了。大家使用过程序中,如果没有效果,欢迎反馈给我。我们一起进步。
//clearFocus等同于隐藏软键盘 public static void clearFocus(Component component){ Component focusedComponent = findFocus(component, Component.FOCUS_NEXT);//尝试向后找焦点控件 if (focusedComponent != null) { focusedComponent.clearFocus(); return; } focusedComponent = findFocus(component, Component.FOCUS_PREVIOUS);//尝试向前找焦点控件 if (focusedComponent != null) { focusedComponent.clearFocus(); } } //找焦点控件 public static Component findFocus(Component component, int direction){ if (component.hasFocus()) { return component; } Component focusableComponent = component; int i = 99; while (i-->0){ focusableComponent = focusableComponent.findNextFocusableComponent(direction); if (focusableComponent != null) { if (focusableComponent.hasFocus()) { return focusableComponent; } } else { break; } } return null; }
隐藏软键盘(隐藏输入法)的实现方法
微信扫码分享
大哥,那软键盘弹出后 输入框被遮挡有什么好的解决办法吗
这个问题我没有研究过。
输入框被遮挡 你可以把你的布局写成超出滚动的 那样 你的输入框永远不会被挡
1. 先设置getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_PAN);
2. 布局文件用ScrollView包起来
3. 监听ScrollView的布局状态,其可视范围变小了,证明输入法弹出了。
4. 滚动ScrollView到当前焦点控件的下面。
亲测可行!
getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_PAN); super.setUIContent(ResourceTable.Layout_ability_textfield); Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(getContext()); Point pt = new Point(); display.get().getSize(pt); int screenHeight = pt.getPointYToInt(); ScrollView root = (ScrollView) findComponentById(ResourceTable.Id_root); root.setLayoutRefreshedListener(new Component.LayoutRefreshedListener() { @Override public void onRefreshed(Component component) { Timber.d("onRefreshed() called with: component = [ %s ]", component); Rect rect = new Rect(); component.getWindowVisibleRect(rect); Timber.d("onRefreshed() called with: rect = [ %s ]", rect); int softHeight = screenHeight - rect.bottom ; Timber.d("onRefreshed() called with: softHeight = [ %s ]", softHeight); if (softHeight>100){//当输入法高度大于100判定为输入法打开了 Component focusView = root.findFocus(); focusView.getWindowVisibleRect(rect); root.fluentScrollYTo(rect.bottom); } } });
滚动的距离这样计算不太对。你自己调整一下
输入框被软键盘遮挡的解决办法
感谢!!!
大哥,那软键盘弹出后 输入框被遮挡有什么好的解决办法吗
这个问题我没有研究过。
输入框被遮挡 你可以把你的布局写成超出滚动的 那样 你的输入框永远不会被挡
1. 先设置getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_PAN);
2. 布局文件用ScrollView包起来
3. 监听ScrollView的布局状态,其可视范围变小了,证明输入法弹出了。
4. 滚动ScrollView到当前焦点控件的下面。
亲测可行!
滚动的距离这样计算不太对。你自己调整一下
输入框被软键盘遮挡的解决办法
感谢!!!