/** * 让登陆按钮不被输入键盘挡住
在这里引用这个方法的时候 第一个参数传入根布局的View 如LinerLayout的View对象.第二个参数传入登陆按钮的View即可 */
private boolean isFirst = true;//这个设置是为了只当软件盘弹起时才去测量
private void addLayoutListenner(final View main, final View scroll) { main.getViewTreeObserver() .addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Rect rect = new Rect(); //1.获取窗口可视区域 main.getWindowVisibleDisplayFrame(rect); //在键盘没弹起来时,获取main窗体的不可视区域的高度 int mainInvisableHeight = main.getRootView() .getHeight() - rect.bottom; //窗体的高度 int screenHeight = main.getRootView() .getHeight(); //3.不可见区域大于屏幕1/4,说明键盘弹起 if (mainInvisableHeight > screenHeight / 4) { if (isFirst) { int[] location = new int[2]; scroll.getLocationInWindow(location); //4.获取scroll窗体的坐标,算出main需要滚动的高度 int scrollHeight = (location[1] + scroll.getHeight() - rect.bottom); //5.让界面整体上移键盘的高度 main.scrollTo(0, scrollHeight); isFirst = false; } } else { //不可见区域小于屏幕1/4说明键盘隐藏,将界面移回原来的高度 main.scrollTo(0, 0); isFirst = true; } } }); }