应用场景 :直播中,弹出一个编辑框的dialog,软件盘收起并且隐藏掉当前的dialog
1、自定义一个带编辑框的dailog
2、利用handler进行定期查询dailog外层View的屏幕位置,当位置发生变化后,判断位置Y(距离顶部位置)是向下还是向上变化;即认为
private Handler handler = new Handler(); int[] location = new int[2]; private Runnable runnable = new Runnable() { @Override public void run() { int[] location2 = new int[2]; mParentView.getLocationOnScreen(location2); if (location[1] != location2[1] && location[1] != 0) { if (location[1] - location2[1] < 0) { LogUtils.d("收起"); location = location2; dismiss(); } else { LogUtils.d("弹出"); location = location2; } LogUtils.d("location2[1]>>>" + location2[1]); } if (location[1] == 0){ location = location2; } handler.postDelayed(runnable, 50); } };
3、在软键盘显示的时候进行轮询位置变化
@Override public void show() { super.show(); if (mEtChatRoomMessage != null) { mEtChatRoomMessage.setText(""); mEtChatRoomMessage.postDelayed(() -> { showKeyBroad(); handler.postDelayed(runnable, 100); }, 50); } }
4、必须要在dialog隐藏的时候取消handler的回调,否则会发生异常情况哦
setOnDismissListener(dialog -> { handler.removeCallbacksAndMessages(null); });