Android 监听键盘弹出关闭

来源:互联网 发布:jquery 1.4.1.js 下载 编辑:程序博客网 时间:2024/06/10 11:23

Android源生并不支持监听键盘的开关,一般我们都是通过监听Layout的变化来实现

效果:
这里写图片描述
1.在Manifest文件中当前activity下面设置如下属性:
这里写图片描述
2.在键盘弹出时会改变大小的布局上设置监听
这里写图片描述
3.在监听方法中判断键盘是开启还是关闭状态.

        int[] i2 = new int[2];        overscroll.getLocationInWindow(i2);//得到当前控件在屏幕中的位置        if(overscroll.getHeight()< maxHeight-10){//          软键盘可见            hideAllExceptFocusView();//在软键盘显示时,隐藏除具有焦点外的View        }else if((overscroll.getHeight()> maxHeight+10 || overscroll.getHeight() == maxHeight)&&i2[1] > top){//控件高度变大了,且向下移动了.//          软键盘隐藏            showAllView();//在软键盘隐藏时,显示所有View            overscroll.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {                @Override                public void onGlobalLayout() {                    overscroll.getViewTreeObserver().removeGlobalOnLayoutListener(this);                    View focus = overscroll.findFocus();                    if(focus != null){                        //软键盘关闭时,滚动到存在焦点的控件的位置                        overscroll.scrollTo(overscroll.getScrollX(), ((View)focus.getParent()).getTop()+et_category.getHeight());                    }                }            });        }        maxHeight = overscroll.getHeight();        int[] i1 = new int[2];        overscroll.getLocationInWindow(i1);        top = i1[1];//保存上一次顶部的位置(方便下次判断控件是被顶起还是落下)
0 0