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三方库

目录
相关文章
|
14天前
|
Linux 开发工具 iOS开发
Codex保姆级教程(下载+安装+登录+使用,含所有版本,2026最新)
Codex是OpenAI推出的开源AI编程智能体,本地运行,可理解整个代码库,支持自然语言指令改代码、执行命令、跑测试、修Bug,兼容Win/macOS/Linux。(239字)
6115 45
|
2天前
|
云安全 人工智能 安全
|
15天前
|
JavaScript API iOS开发
DeepSeek Harness下载、安装、配置一篇搞定(2026整理,送常用插件)
DeepSeek Harness(DSH)是DeepSeek于2026年开源的MIT协议AI Agent框架,支持插件化组装模型、工具、沙箱与UI,兼容主流大模型。本文提供免Node.js的桌面端安装包(Win/macOS),含详细图文教程及热门插件推荐。
5088 60
|
9天前
|
Linux 开发工具 iOS开发
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
本文详解OpenAI Codex国内快速上手指南:提供Win/macOS/Linux全平台免安装包、npm/Homebrew等多方式安装、账号/API Key双登录、简体中文切换,覆盖下载、安装、汉化、使用及高频避坑(如微软商店失败、npm超时等),10分钟即可跑通。
2437 6
|
1天前
|
JSON 前端开发 API
阿里云最新Qwen4-Max大模型介绍:模型定位、特征、能力、适用场景解析
2026 年 11 月 11 日,阿里发布第四代旗舰模型Qwen4-Max预览版,首发千问 AI 平台 Token Plan,并同步接入千问办公、Qoder 等入口。该版本聚焦知识理解、世界感知、编程与 Agentic 四项升级,主打复杂编程、专业办公、前端设计与专业软件操作。预览期至 11 月 25 日 24:00,到期后模型标识下线;限时 2 折、新用户再享 5 折,最低 1 折可体验。
|
7天前
|
人工智能 安全 API
DeepSeek Harness下载、安装、配置一篇搞定(2026整理,送常用插件)
DeepSeek Harness(dsh)是DeepSeek AI于2026年开源的轻量级Agent运行框架,基于Cordis内核,践行“一切皆插件”理念。支持Web/桌面双端,开箱即用;内置多模型接入、丰富插件生态(超3.5万),适配编程、办公、创作等场景。(239字)
1523 1

热门文章

最新文章