可以监听输入法键盘的打开和关闭动作,通过回调精准判断,兼容几乎所有手机
原理:
在输入法打开和关闭的时候会引起当前界面的高度变化,从而出发onLayout方法,所以我们监听layout动作,当触发此方法时,判断当前界面可视部分的高度变化
package com.shixi.didihr.utils;
import android.app.Activity;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.ViewTreeObserver;
import java.lang.reflect.Method;
/**
* Created by Miller Zhang on 2016/12/27.
* desc:
* github:https://github.com/zxyaust CSDN:http://blog.csdn.net/qq_31340657
* Whatever happens tomorrow,we've had today.
*/
public class ScreenUtil {
private final Activity activity;
private OnInputActionListener listener;
public ScreenUtil(Activity activity) {
this.activity = activity;
}
/**
* @return 底部的虚拟栏的高度
*/
public int getBottomKeyboardHeight() {
int screenHeight = getAccurateScreenDpi()[1];
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
int heightDifference = screenHeight - dm.heightPixels;
return heightDifference;
}
/**
* 获取实际的屏幕尺寸,所有的连同底部虚拟栏
*/
public int[] getAccurateScreenDpi() {
int[] screenWH = new int[2];
Display display = activity.getWindowManager().getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
try {
Class<?> c = Class.forName("android.view.Display");
Method method = c.getMethod("getRealMetrics", DisplayMetrics.class);
method.invoke(display, dm);
screenWH[0] = dm.widthPixels;
screenWH[1] = dm.heightPixels;
} catch (Exception e) {
e.printStackTrace();
}
return screenWH;
}
/**
* 监听某个页面上输入法键盘打开动作
*
* @param view 要实现监听的页面上的任意一个view都可以
* @param listener 监听接口
*/
public void observeInputlayout(final View view, OnInputActionListener listener) {
this.listener = listener;
view.postDelayed(new Runnable() {
@Override
public void run() {
observe(view);
}
}, 500);
}
private void observe(View view) {
view.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
int screenHeight = activity.getWindow().getDecorView().getRootView().getHeight();
int heightDifference = screenHeight - r.bottom;
if (heightDifference > getBottomKeyboardHeight()) {
listener.onOpen();
} else {
listener.onClose();
}
}
});
}
public interface OnInputActionListener {
void onOpen();
void onClose();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
使用方法:
把上面的类复制到自己项目里面,然后调用下面方法:
new ScreenUtil(getActivity()).observeInputlayout(view, new ScreenUtil.OnInputActionListener() {
@Override
public void onOpen() {
startAniout();
}
@Override
public void onClose() {
startAniIn();
}
});