首先注明出处,我只是想做个笔记:http://www.jb51.net/article/64820.htm
第一眼看到的时候,觉得不太可靠,因为之前都是看到这样处理的,根据监听一个根布局的size,可是那个效果不怎么样,我不想用,就想再找找,看到这个的时候就试了试,效果很棒。也学习了
/** * 监听软键盘状态 * * @param activity * @param listener */ public static void addOnSoftKeyBoardVisibleListener(Activity activity, final OnSoftKeyBoardVisibleListener listener) { 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(); boolean visible = (double) displayHight / hight < 0.8; listener.onSoftKeyBoardVisible(visible); } }); }
接口自己定义下就可以了:
public interface OnSoftKeyBoardVisibleListener { void onSoftKeyBoardVisible(boolean visible); }
最后还是谢谢大神
时间: 2024-10-01 06:02:30