@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_by_soft_input_listner);
ViewUtils.inject(this);
et.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//当键盘弹出隐藏的时候会 调用此方法。
@Override
public void onGlobalLayout() {
Rect r = new Rect();
//获取当前界面可视部分
BySoftInputListnerActivity.this.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
//获取屏幕的高度
int screenHeight = BySoftInputListnerActivity.this.getWindow().getDecorView().getRootView().getHeight();
//此处就是用来获取键盘的高度的, 在键盘没有弹出的时候 此高度为0 键盘弹出的时候为一个正数
int heightDifference = screenHeight - r.bottom;
Log.d("Keyboard Size", "Size: " + heightDifference);
showAViewOverKeyBoard(heightDifference);
}
});
}
private void showAViewOverKeyBoard(int heightDifference) {
if (heightDifference > 0) {//显示
if (view == null) {//第一次显示的时候创建 只创建一次
view = View.inflate(this, R.layout.item, null);
RelativeLayout.LayoutParams loginlayoutParams = new RelativeLayout.LayoutParams(-1, -2);
loginlayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
loginlayoutParams.bottomMargin = heightDifference;
rl_root.addView(view, loginlayoutParams);
}
view.setVisibility(View.VISIBLE);
} else {//隐藏
if (view != null) {
view.setVisibility(View.GONE);
}
}
}
软键盘上添加悬浮view
最新推荐文章于 2022-01-21 16:50:02 发布