Android系统开发-获取输入法绑定的View坐标

开发背景:Android 电子白板,需要实现输入法跟随输入功能

  1. 首先要确定输入法绑定的输入框坐标,因为输入法的启动是通过InputMethodManager#startInputInner方法,所以我们在InputMethodManager#startInputInner方法中,用SystemProperties将View的坐标保存:
    InputMethodManager.java
import android.os.SystemProperties;
boolean startInputInner(@InputMethodClient.StartInputReason final int startInputReason,
            IBinder windowGainingFocus, int controlFlags, int softInputMode,
            int windowFlags) {
...
int locationInScreen[] = new int[2];
view.getLocationOnScreen(locationInScreen);
SystemProperties.set("viewPositionInScreen",locationInScreen[0]+"_"+locationInScreen[1]);

}
  1. 最后,在输入法代码中设置键盘需要显示的位置:
int [] viewPositionXY=SystemProperties.get("viewPositionInScreen","0_0").split("_");
//下面是自定义键盘的InputMethodService类设置键盘位置的伪代码 WindowManager.LayoutParams lp = getWindow().getWindow().getAttributes();
            lp.x = viewPositionXY[0] - screenWidth/2;//由于键盘是底部居中,屏幕中间右侧为正x,左侧为负x,screenWidth为屏幕的宽
            lp.y = viewPositionXY[1] - keybaordHeight;//keybaordHeight为键盘的高度

输入法启动的详细流程:Android InputMethod 源码分析,显示输入法流程_JieQiong1的博客-CSDN博客_android 输入法源码

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容