当前位置: 首页 > news >正文

Android 软件盘的弹出和消失的监听

监听接口 OnKeyboardListener.java

    public interface OnKeyboardListener {void onKeyboardHidden();void onKeyboardShow(int keyboardHeight);}
KeyBoardUtil.java
public class KeyBoardUtil {private final static String TAG = "KeyBoardUtil";public  PopupWindow popupWindow;public static void showSoftInput(EditText edit) {edit.setFocusable(true);edit.setFocusableInTouchMode(true);edit.requestFocus();InputMethodManager imm = (InputMethodManager) edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);imm.showSoftInput(edit, 0);edit.setSelection(edit.length());}public static void hideSoftInput(EditText edit) {InputMethodManager imm = (InputMethodManager) edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);imm.hideSoftInputFromWindow(edit.getWindowToken(), 0);}private  ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener;public void observerKeyboard(Activity activity, OnKeyboardListener keyboardListener) {View rootView = new View(activity);popupWindow = new PopupWindow(activity);try {rootView.setLayoutParams(new ViewGroup.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT));popupWindow.setContentView(rootView);popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);popupWindow.setBackgroundDrawable(new ColorDrawable(0));popupWindow.setTouchable(false);popupWindow.showAtLocation(activity.getWindow().getDecorView(), Gravity.NO_GRAVITY, 0, 0);} catch (WindowManager.BadTokenException e) {Log.d(TAG, "observerKeyboard: " , e);}if (onGlobalLayoutListener == null) {onGlobalLayoutListener = getGlobalObserver(rootView, keyboardListener);}rootView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);}private  ViewTreeObserver.OnGlobalLayoutListener getGlobalObserver(View rootView, OnKeyboardListener keyboardListener) {HeightWrapper wrapper = new HeightWrapper();Rect rootRect = new Rect();Context context = rootView.getContext();((Activity)context).getWindow().getDecorView().getWindowVisibleDisplayFrame(rootRect);return () -> {Rect r = new Rect();rootView.getWindowVisibleDisplayFrame(r);int height = r.height();if (wrapper.getHeight() == 0) {wrapper.setHeight(height);} else {if (wrapper.getHeight() != height){Log.i(TAG,"keyboard wrapper height = "+wrapper.getHeight() + "heigth = "+height);int diff = wrapper.getHeight() - height;if (diff < -200 && rootRect.bottom == r.bottom) {keyboardListener.onKeyboardHidden();}else {keyboardListener.onKeyboardShow(ScreenUtils.getScreenRealHeight() - r.bottom);}wrapper.setHeight(height);}}};}public void clearObserver() {onGlobalLayoutListener = null;if (popupWindow != null) {popupWindow.dismiss();popupWindow.getContentView().getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);}popupWindow = null;}static class HeightWrapper {int height = 0;public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}}}

在activity 中初始化完UI以后,添加如下代码:

        view.viewTreeObserver.addOnGlobalLayoutListener {if (mKeyBoardUtil == null) {mKeyBoardUtil = KeyBoardUtil()mKeyBoardUtil?.observerKeyboard(requireActivity(),object : KeyBoardUtil.OnKeyboardListener {override fun onKeyboardHidden() {Log.i(TAG, "onKeyboardHidden")}override fun onKeyboardShow(keyboardHeight: Int) {Log.i(TAG, "onKeyboardShow")}})}}

 在activity ondestory中调用:

        mKeyBoardUtil?.clearObserver()

http://www.lryc.cn/news/337469.html

相关文章:

  • 通俗易懂HTTP和HTTPS区别
  • 【ZZULIOJ】1061: 顺序输出各位数字(Java)
  • java数据结构与算法刷题-----LeetCode260. 只出现一次的数字 III
  • AWS被误扣费了,怎么解决?
  • 再传IPO消息,SHEIN的上市为何充满变数?
  • maven bom
  • 若依vue中关于字典的使用
  • 链表题(哑结点的使用)
  • C#:求三个整数的最大值
  • 广州南沙番禺联想SR530服务器主板传感器故障维修
  • 深入探索自然语言处理:用Python和BERT构建文本分类模型
  • 在Visual Studio Code中编辑React项目时,以下是一些推荐的扩展
  • 智算时代的基础设施如何实现可继承可演进?浪潮云海发布 InCloud OS V8 新一代架构平台
  • LDF、DBC、BIN、HEX、S19、BLF、ARXML、slx等
  • 因为使用ArrayList.removeAll(List list)导致的机器重启
  • Let‘s Encrypt
  • C语言 | Leetcode C语言题解之第24题两两交换链表中的节点
  • 【LeetCode热题100】【回溯】电话号码的字母组合
  • 解析mysql的DDL语句生成高斯内表及表字段主键配置
  • ANSYS Electromagnetics Suite 2023 R2 三维电磁(EM)仿真软件下载
  • pbootcms百度推广链接打不开显示404错误页面
  • springboot 整合 swagger2
  • redis-缓存穿透与雪崩
  • K8S临时存储-本地存储-PV和PVC的使用-动态存储(StorageClass)
  • jeecg-boot安装
  • Unity面经(自整)——移动开发与Shader
  • Nginx实现反向代理、负载均衡、动静分离
  • 【Linux】网络基础(一)
  • 前端小白学习Vue框架(二)
  • 飞书api增加权限