鸿蒙NEXT开发键盘工具类(ArkTs)

复制代码
export declare type KeyboardCallBack = (show: boolean, height: number) => void;
import { AppUtil } from './AppUtil';
import { LogUtil } from './LogUtil';
import { ArrayUtil } from './ArrayUtil';

/**
 * 键盘工具类
 * @author 鸿蒙布道师
 * @since 2025/04/18
 */
export class KeyboardUtil {
  private static callBacks: KeyboardCallBack[] = []; // 缓存的监听回调
  private static keyboardCallBack: ((height: number) => void) | undefined; // 全局键盘监听回调

  /**
   * 拉起键盘
   * @param key 输入框类组件的key或id
   * @param uiContext 页面的UIContext(子窗口需传入,主窗口可省略)
   */
  static show(key: string, uiContext?: UIContext): void {
    (uiContext ?? AppUtil.getUIContext()).getFocusController().requestFocus(key);
  }

  /**
   * 关闭键盘
   * @param uiContext 页面的UIContext(子窗口需传入,主窗口可省略)
   */
  static hide(uiContext?: UIContext): void {
    (uiContext ?? AppUtil.getUIContext()).getFocusController().clearFocus();
  }

  /**
   * 订阅输入法软键盘显示或隐藏事件
   * @param callback 回调函数:
   *         - show: boolean,true表示键盘显示,false表示键盘隐藏;
   *         - height: number,键盘高度。
   */
  static onKeyboardListener(callback: KeyboardCallBack): void {
    if (ArrayUtil.contain(KeyboardUtil.callBacks, callback)) {
      LogUtil.error(`KeyboardUtil-onKeyboardListener: 监听事件已存在!`);
      return;
    }

    KeyboardUtil.callBacks.push(callback);

    if (!KeyboardUtil.keyboardCallBack) {
      KeyboardUtil.keyboardCallBack = (height: number) => {
        KeyboardUtil.callBacks.forEach(cb => cb?.(height > 0, height));
      };
      AppUtil.getMainWindow().on('keyboardHeightChange', KeyboardUtil.keyboardCallBack);
    }
  }

  /**
   * 取消订阅输入法软键盘显示或隐藏事件
   * @param callback 要移除的监听事件,若不传则移除所有监听事件
   */
  static removeKeyboardListener(callback?: KeyboardCallBack): void {
    if (callback) {
      ArrayUtil.remove(KeyboardUtil.callBacks, callback);
    } else {
      KeyboardUtil.callBacks = [];
    }

    if (KeyboardUtil.keyboardCallBack && KeyboardUtil.callBacks.length === 0) {
      AppUtil.getMainWindow().off('keyboardHeightChange', KeyboardUtil.keyboardCallBack);
      KeyboardUtil.keyboardCallBack = undefined;
    }
  }
}

代码如下:
TypeScript 复制代码
export declare type KeyboardCallBack = (show: boolean, height: number) => void;
import { AppUtil } from './AppUtil';
import { LogUtil } from './LogUtil';
import { ArrayUtil } from './ArrayUtil';

/**
 * 键盘工具类
 * @author 鸿蒙布道师
 * @since 2025/04/18
 */
export class KeyboardUtil {
  private static callBacks: KeyboardCallBack[] = []; // 缓存的监听回调
  private static keyboardCallBack: ((height: number) => void) | undefined; // 全局键盘监听回调

  /**
   * 拉起键盘
   * @param key 输入框类组件的key或id
   * @param uiContext 页面的UIContext(子窗口需传入,主窗口可省略)
   */
  static show(key: string, uiContext?: UIContext): void {
    (uiContext ?? AppUtil.getUIContext()).getFocusController().requestFocus(key);
  }

  /**
   * 关闭键盘
   * @param uiContext 页面的UIContext(子窗口需传入,主窗口可省略)
   */
  static hide(uiContext?: UIContext): void {
    (uiContext ?? AppUtil.getUIContext()).getFocusController().clearFocus();
  }

  /**
   * 订阅输入法软键盘显示或隐藏事件
   * @param callback 回调函数:
   *         - show: boolean,true表示键盘显示,false表示键盘隐藏;
   *         - height: number,键盘高度。
   */
  static onKeyboardListener(callback: KeyboardCallBack): void {
    if (ArrayUtil.contain(KeyboardUtil.callBacks, callback)) {
      LogUtil.error(`KeyboardUtil-onKeyboardListener: 监听事件已存在!`);
      return;
    }

    KeyboardUtil.callBacks.push(callback);

    if (!KeyboardUtil.keyboardCallBack) {
      KeyboardUtil.keyboardCallBack = (height: number) => {
        KeyboardUtil.callBacks.forEach(cb => cb?.(height > 0, height));
      };
      AppUtil.getMainWindow().on('keyboardHeightChange', KeyboardUtil.keyboardCallBack);
    }
  }

  /**
   * 取消订阅输入法软键盘显示或隐藏事件
   * @param callback 要移除的监听事件,若不传则移除所有监听事件
   */
  static removeKeyboardListener(callback?: KeyboardCallBack): void {
    if (callback) {
      ArrayUtil.remove(KeyboardUtil.callBacks, callback);
    } else {
      KeyboardUtil.callBacks = [];
    }

    if (KeyboardUtil.keyboardCallBack && KeyboardUtil.callBacks.length === 0) {
      AppUtil.getMainWindow().off('keyboardHeightChange', KeyboardUtil.keyboardCallBack);
      KeyboardUtil.keyboardCallBack = undefined;
    }
  }
}
相关推荐
我命由我123451 分钟前
Android Studio 提示信息 ‘equals(““)‘ can be replaced with ‘isEmpty()‘
android·ide·android studio·安卓·android jetpack·android-studio·android runtime
Bryce李小白10 分钟前
Flutter实现Android原生相机拍照
android·数码相机·flutter
初学者-Study11 分钟前
Android基础(一) 运行HelloWorld
android·helloworld·模拟器运行
杂雾无尘28 分钟前
SwiftUI 动画新技能,让你的应用「活」起来!
ios·swiftui·swift
BUG创建者1 小时前
openlayer根据不同的状态显示不同的图层颜色
android·java·javascript
用户2018792831671 小时前
浅谈画框ImageView的background和src属性的差异
android
林大鹏天地1 小时前
iOS18系统 [YYKVStorage _dbClose] 偶现崩溃
ios
林大鹏天地1 小时前
使用Xcode16打包后,App在暗夜模式下,iOS18 切换Tabbar的item,会有一根白色线条闪过。
ios
2501_915909062 小时前
iOS 加固工具实战解析,主流平台审核机制与工具应对策略
android·ios·小程序·https·uni-app·iphone·webview
马 孔 多 在下雨2 小时前
安卓服务与多线程
android