您的位置:首页 > 移动开发 > Android开发

Android 监听键盘弹出关闭

2015-08-24 15:51 756 查看

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];//保存上一次顶部的位置(方便下次判断控件是被顶起还是落下)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: