尝试了N多种监听键盘弹出/隐藏的监听方法,终于找到实用的方法,再次记录一下:
给页面的最外层布局添加布局监听事件:
//获取屏幕高度,初始化定义键盘的高度
int screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
//阀值设置为屏幕高度的1/3
keyHeight = screenHeight / 3;
rl_event.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
}
});
在监听方法里实现自己具体的操作:
/**
* 监听键盘弹出/隐藏
*/
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
//old是改变前的左上右下坐标点值,没有old的是改变后的左上右下坐标点值
//现在认为只要控件将Activity向上推的高度超过了1/3屏幕高,就认为软键盘弹起
if (oldBottom != 0 && bottom != 0 && (oldBottom - bottom > keyHeight)) {
RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams) et_event
.getLayoutParams(); // 取控件当前的布局参数
linearParams.height = UIUtils.dip2px(100);
linearParams.width = LinearLayout.LayoutParams.MATCH_PARENT;// 控件的宽
et_event.setLayoutParams(linearParams); // 使设置好的布局参数应用到控件
} else if (oldBottom != 0 && bottom != 0 && (bottom - oldBottom > keyHeight)) {
RelativeLayout.LayoutParams linearParams = (RelativeLayout.LayoutParams) et_event
.getLayoutParams(); // 取控件当前的布局参数
linearParams.height = LinearLayout.LayoutParams.MATCH_PARENT;
linearParams.width = LinearLayout.LayoutParams.MATCH_PARENT;// 控件的宽
et_event.setLayoutParams(linearParams); // 使设置好的布局参数应用到控件
}
}