监测软键盘的弹出和收起
Posted 潇潇微雨up
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了监测软键盘的弹出和收起相关的知识,希望对你有一定的参考价值。
boolean isVisiableForLast = false;
int keyboardHeight = 0;
public void addOnSoftKeyBoardVisibleListener(Activity activity)
final View decorView = activity.getWindow().getDecorView();
decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
@Override
public void onGlobalLayout()
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
//计算出可见屏幕的高度
int displayHight = rect.bottom - rect.top;
//获得屏幕整体的高度
int hight = decorView.getHeight();
//获得键盘高度
keyboardHeight = hight-displayHight;
boolean visible = (double) displayHight / hight < 0.8;
if(visible != isVisiableForLast)
// listener.onSoftKeyBoardVisible(visible,keyboardHeight );
if (visible)//软键盘显示
else //软键盘隐藏
isVisiableForLast = visible;
);
以上是关于监测软键盘的弹出和收起的主要内容,如果未能解决你的问题,请参考以下文章