public void addSoftKeyBoardListener(final Activity activity){
//android 11以上通过监听动画计算系统键盘高度
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
invokeAbove31(activity);
return;
}
SoftKeyBoardListener.setListener(activity,new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
@Override
public void keyBoardShow(final int height) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
int virtualBarHeight = GetNotConsistentHeight() - getNavigationBarHeight(activity);
sendKeyBoardChangeMsg(1,Math.max(virtualBarHeight, height));
Log.d("invokeAbove31","keyBoardShow virtualBarHeight:"+virtualBarHeight+",height:"+height);
}
}, 300);
}
@Override
public void keyBoardHide(int height) {
sendKeyBoardChangeMsg(0,height);
Log.d("invokeAbove31","keyBoardHide :height:"+height);
}
});
}
public void sendKeyBoardChangeMsg(int showFlag,int height){
JSONObject theJSonParam = new JSONObject();
try {
theJSonParam.put("showFlag",showFlag);
theJSonParam.put("height",height);
theJSonParam.put("density", getDensity());
//发消息通知显示隐藏系统键盘
//sendMsg(KEY_TdxSysKeyBoardStateChange,theJSonParam.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
int height;
@RequiresApi(api = 30)
private void invokeAbove31(Activity activity) {
activity.getWindow().getDecorView().setWindowInsetsAnimationCallback(new WindowInsetsAnimation.Callback(DISPATCH_MODE_STOP) {
@NonNull
@Override
public WindowInsets onProgress(@NonNull WindowInsets windowInsets, @NonNull List<WindowInsetsAnimation> list) {
int imeHeight = windowInsets.getInsets(WindowInsetsCompat.Type.ime()).bottom;
int navHeight = windowInsets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom;
boolean hasNavigationBar = windowInsets.isVisible(WindowInsetsCompat.Type.navigationBars()) &&
windowInsets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom > 0;
height = hasNavigationBar ? Math.max(imeHeight - navHeight, 0) : imeHeight;
return windowInsets;
}
@Override
public void onEnd(@NonNull WindowInsetsAnimation animation) {
super.onEnd(animation);
// 键盘弹起height>0 键盘收起height == 0
sendKeyBoardChangeMsg(height > 0 ? 1 : 0,height);
}
});
}
public int GetNotConsistentHeight()//有些虚拟键在某些情况下导致高度不一致 影响布局
{
if(getWindow()==null||getWindow().getDecorView()==null)
return 0;
int height = 0;
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int fromToBottomHeight = frame.bottom;
int screenHeigh = getHeightDpi();
height = screenHeigh - fromToBottomHeight;
return height;
}
/**
* 获取虚拟按键的高度
*/
public int getNavigationBarHeight(Activity activity) {
int result = 0;
if (hasNavBar(activity)) {
Resources res = activity.getResources();
int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
result = res.getDimensionPixelSize(resourceId);
}
}
return result;
}
public float getDensity(){
// 获取当前Activity的DisplayMetrics对象
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
// 从DisplayMetrics中获取density
return displayMetrics.density;
}
网友评论