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];//保存上一次顶部的位置(方便下次判断控件是被顶起还是落下)
版权声明:本文为g707175425原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。