harmony-utils之KeyboardUtil,键盘工具类

简介: harmony-utils 是一款功能强大的 HarmonyOS 工具库,提供丰富的工具类以提升开发效率。其子库 picker_utils 包含 PickerUtil、PhotoHelper 和 ScanUtil,适用于各类应用开发场景。

harmony-utils之KeyboardUtil,键盘工具类

harmony-utils 简介与说明


harmony-utils 一款功能丰富且极易上手的HarmonyOS工具库,借助众多实用工具类,致力于助力开发者迅速构建鸿蒙应用。其封装的工具涵盖了APP、设备、屏幕、授权、通知、线程间通信、弹框、吐司、生物认证、用户首选项、拍照、相册、扫码、文件、日志,异常捕获、字符、字符串、数字、集合、日期、随机、base64、加密、解密、JSON等一系列的功能和操作,能够满足各种不同的开发需求。
picker_utils 是harmony-utils拆分出来的一个子库,包含PickerUtil、PhotoHelper、ScanUtil。

下载安装
ohpm i @pura/harmony-utils
ohpm i @pura/picker_utils

  //全局初始化方法,在UIAbility的onCreate方法中初始化 AppUtil.init()
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    AppUtil.init(this.context);
  }

API方法与使用


show 拉起键盘
KeyboardUtil.show("id_input_1000");
hide 隐藏键盘
 KeyboardUtil.hide();
onKeyboardListener 订阅输入法软键盘显示和隐藏事件
KeyboardUtil.onKeyboardListener((show, height) => {
  LogUtil.error("onKeyboardListener-B: " + height);
  if (show) {
    ToastUtil.showToast(`键盘显示,高度为:${height}`);
  } else {
    ToastUtil.showToast(`键盘已关闭!`);
  }
});
removeKeyboardListener 取消订阅输入法软键盘显示或隐藏事件
KeyboardUtil.removeKeyboardListener(); //移除所有
KeyboardUtil.removeKeyboardListener(callback); //移除指定

示例代码


import {
    router } from '@kit.ArkUI';
import {
    MockSetup } from '@ohos/hamock';
import {
    KeyboardCallBack, KeyboardUtil, LogUtil, ToastUtil } from '@pura/harmony-utils';
import {
    TitleBarView } from '../../component/TitleBarView';
import {
    DescribeBean } from '../../model/DescribeBean';

/**
 * 键盘工具类
 */
@Entry
@Component
struct Index {
   
  private scroller: Scroller = new Scroller();
  private controller: TextInputController = new TextInputController()
  @State describe: DescribeBean = router.getParams() as DescribeBean;
  private callback: KeyboardCallBack = (show, height) => {
   
    LogUtil.error("onKeyboardListener-A: " + height);
    if (show) {
   
      ToastUtil.showToast(`键盘显示!高度为:${
     height}`);
    } else {
   
      ToastUtil.showToast(`键盘已关闭!`);
    }
  };

  @MockSetup
  mock() {
   
    this.describe = new DescribeBean("KeyboardUtil", "键盘工具类");
  }

  build() {
   
    Column() {
   
      TitleBarView({
    describe: this.describe })
      Divider()
      Scroll(this.scroller) {
   
        Column() {
   
          Button("show(),主动拉起键盘")
            .btnStyle()
            .onClick(() => {
   
              KeyboardUtil.show("id_input_1000");
            })
          Button("hide(),关闭键盘")
            .btnStyle()
            .onClick(() => {
   
              KeyboardUtil.hide();
            })
          Button("onKeyboardListener()")
            .btnStyle()
            .onClick(() => {
   
              KeyboardUtil.onKeyboardListener((show, height) => {
   
                LogUtil.error("onKeyboardListener-B: " + height);
                if (show) {
   
                  ToastUtil.showToast(`键盘显示,高度为:${
     height}`);
                } else {
   
                  ToastUtil.showToast(`键盘已关闭!`);
                }
              });
            })
          Button("removeKeyboardListener()")
            .btnStyle()
            .onClick(() => {
   
              KeyboardUtil.removeKeyboardListener(); //移除所有
            })
          Button("onKeyboardListener()-指定")
            .btnStyle()
            .onClick(() => {
   
              KeyboardUtil.onKeyboardListener(this.callback);
            })
          Button("removeKeyboardListener()-指定")
            .btnStyle()
            .onClick(() => {
   
              KeyboardUtil.removeKeyboardListener(this.callback); //移除指定
            })

          TextInput({
    placeholder: '请输入', controller: this.controller })
            .margin({
    top: 30 })
            .width('90%')
            .id("id_input_1000")

          Blank().layoutWeight(1)
        }
        .margin({
    top: 5, bottom: 5 })
      }
      .layoutWeight(1)
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Start)
    .backgroundColor($r('app.color.main_background'))
  }
}


@Styles
function btnStyle() {
   
  .width('90%')
  .margin({
    top: 10, bottom: 5 })
}

创作不易,请给童长老点赞👍


https://github.com/787107497/harmony-utils
https://gitee.com/tongyuyan/harmony-utils
OpenHarmony三方库

目录
相关文章
|
API Apache 开发者
最受欢迎的三方库之EventBus
EventBus 是一款支持 Sticky 和跨 App 广播的消息总线,适用于 HarmonyOS 开发。提供丰富的 API,如事件监听、消息发布等,助力高效构建应用。
256 0
|
9月前
|
API Apache Android开发
最受欢迎的三方库之harmony-web
`harmony-web` 是基于鸿蒙 WebView 构建的轻量级库,提供丰富的功能与问题解决方案,简化 WebView 开发。结合 `harmony-utils` 与 `harmony-dialog`,可快速构建高效、易用的鸿蒙应用。支持 ArkWeb 组件与多种配置选项,助力开发者高效实现网页加载、交互及弹窗功能。开源协议为 Apache License 2.0。
280 0
|
JSON API Apache
最受欢迎的三方库之china_area
`@nutpi/china_area` 是一款提供中国省、市、县三级区域数据的 OpenHarmony 工具库,支持同步与异步调用方式,便于快速集成至鸿蒙应用中。配套 `@pura/harmony-utils` 与 `@pura/harmony-dialog` 可实现高效开发,如地区选择器等交互功能。API 简洁易用,附有完整示例代码,助力开发者提升效率。项目遵循 Apache License 2.0 协议,欢迎参与共建。
253 0
|
JSON 生物认证 API
harmony-utils之RegexUtil,正则工具类
RegexUtil 是 harmony-utils 中的正则工具类,提供多种常用正则验证方法,如验证手机号、邮箱、身份证、数字、URL 等。支持快速判断字符串是否匹配特定格式,助力 HarmonyOS 开发高效便捷。
250 0
|
JSON 前端开发 生物认证
harmony-utils之SnapshotUtil,截图相关工具类
harmony-utils是一款专为HarmonyOS开发的高效工具库,提供丰富的实用功能,助力开发者快速构建鸿蒙应用。其中的SnapshotUtil类专注于截图功能,支持组件截图、自定义Builder渲染截图、窗口截图及系统截屏监听等操作,极大提升了开发效率与应用交互体验。
245 0
|
JSON 生物认证 API
harmony-utils之TempUtil,温度转换工具类
harmony-utils 是一款功能丰富的 HarmonyOS 工具库,提供 TempUtil 温度转换工具类,支持摄氏度、华氏度与开尔文之间的相互转换,助力开发者高效构建鸿蒙应用。
261 0
|
JSON 生物认证 API
harmony-utils之JSONUtil,JSON工具类
harmony-utils之JSONUtil是一款HarmonyOS平台下的高效JSON处理工具类,支持JSON字符串与对象、数组、Map之间的相互转换,简化数据解析与操作,提升开发效率。
272 0
|
JSON 搜索推荐 生物认证
harmony-utils之ToastUtil,吐司工具类
harmony-utils 是一款功能强大的 HarmonyOS 工具库,包含丰富的工具类,如吐司(ToastUtil)、设备、通知、线程通信等,帮助开发者高效开发鸿蒙应用。支持快速集成与个性化配置,提升开发效率。
227 0
|
缓存 JSON 算法
harmony-utils之LRUCacheUtil,LRUCache缓存工具类
harmony-utils 是一款功能丰富的 HarmonyOS 工具库,提供多种实用工具类,助力开发者快速构建鸿蒙应用。其中 LRUCacheUtil 是基于 LRU 算法的缓存工具类,支持获取、添加、删除、清空缓存等操作,并可自定义缓存容量,适用于高效管理内存缓存场景。
193 0
|
存储 JSON 生物认证
harmony-utils之AssetUtil,关键资产存储服务工具类
AssetUtil 是 harmony-utils 工具库中的关键资产存储服务工具类,提供新增、查询、删除资产等功能,支持同步与异步操作,适用于 HarmonyOS 应用开发。
228 0

热门文章

最新文章