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;
}
适配Android11键盘的高度
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.先添加对键盘的监听 当系统消息出现UIKeyboardWillShowNotification和UIKeybo...
- 虽然最新的键盘不再使用keyboardview开发,但是以前的代码还是要维护,现在接了一个任务 ,要完成pad的适...
- 最近在项目中,需要获取到软键盘的高度,再将底部的布局上移键盘的高度,话不多说,直接上代码: 键盘的打开与关闭操作:...
- Android 软键盘高度获取 概述 在Android开发中,有时候我们需要获取软键盘的高度,以便进行相应的布局调...
- #pragma mark - reg & unreg notification - (void)regNotifi...