目录

OpenHarmony 实战开发 —— 在自绘编辑框中使用输入法

在输入法框架中,可以通过 getController 方法获取到 InputMethodController 实例来绑定输入法并监听输入法应用的各种操作,比如插入、删除、选择、光标移动等。这样就可以在自绘编辑框中使用输入法,并实现更加灵活和自由的编辑操作。

开发步骤

  1. 开发者在自绘编辑框使用输入法时,首先需要在DevEco Studio工程中新建一个ets文件,命名为自定义控件的名称,本示例中命名为CustomInput,在文件中定义一个自定义控件,并从@kit.IMEKit中导入inputMethod。
ts 复制代码
    import { inputMethod } from '@kit.IMEKit';

    @Component
    export struct CustomInput {
      build() {
      }
    }
  1. 在控件中,使用Text组件作为自绘编辑框的文本显示组件,使用状态变量inputText作为Text组件要显示的内容。
ts 复制代码
    import { inputMethod } from '@kit.IMEKit';

    @Component
    export struct CustomInput {
      @State inputText: string = ''; // inputText作为Text组件要显示的内容。

      build() {
        Text(this.inputText) // Text组件作为自绘编辑框的文本显示组件。
          .fontSize(16)
          .width('100%')
          .lineHeight(40)
          .id('customInput')
          .height(45)
          .border({ color: '#554455', radius: 30, width: 1 })
          .maxLines(1)
      }
    }

DD一下: 欢迎大家关注公众号<程序猿百晓生>,可以了解到以下知识点。

erlang 复制代码
`欢迎大家关注公众号<程序猿百晓生>,可以了解到以下知识点。`
1.OpenHarmony开发基础
2.OpenHarmony北向开发环境搭建
3.鸿蒙南向开发环境的搭建
4.鸿蒙生态应用开发白皮书V2.0 & V3.0
5.鸿蒙开发面试真题(含参考答案) 
6.TypeScript入门学习手册
7.OpenHarmony 经典面试题(含参考答案)
8.OpenHarmony设备开发入门【最新版】
9.沉浸式剖析OpenHarmony源代码
10.系统定制指南
11.【OpenHarmony】Uboot 驱动加载流程
12.OpenHarmony构建系统--GN与子系统、部件、模块详解
13.ohos开机init启动流程
14.鸿蒙版性能优化指南
.......
  1. 在控件中获取inputMethodController实例,并在文本点击时调用controller示例的attach方法绑定和拉起软键盘,并注册监听输入法插入文本、删除等方法,本示例仅展示插入、删除。
ts 复制代码
    import { inputMethod } from '@kit.IMEKit';

    @Component
    export struct CustomInput {
      @State inputText: string = ''; // inputText作为Text组件要显示的内容。
      private isAttach: boolean = false;
      private inputController: inputMethod.InputMethodController = inputMethod.getController();

      build() {
        Text(this.inputText) // Text组件作为自绘编辑框的文本显示组件。
          .fontSize(16)
          .width('100%')
          .lineHeight(40)
          .id('customInput')
          .onBlur(() => {
            this.off();
          })
          .height(45)
          .border({ color: '#554455', radius: 30, width: 1 })
          .maxLines(1)
          .onClick(() => {
            this.attachAndListener(); // 点击控件
          })
      }

      async attachAndListener() { // 绑定和设置监听
        focusControl.requestFocus('CustomInput');
        await this.inputController.attach(true, {
          inputAttribute: {
            textInputType: inputMethod.TextInputType.TEXT,
            enterKeyType: inputMethod.EnterKeyType.SEARCH
          }
        });
        if (!this.isAttach) {
          this.inputController.on('insertText', (text) => {
            this.inputText += text;
          })
          this.inputController.on('deleteLeft', (length) => {
            this.inputText = this.inputText.substring(0, this.inputText.length - length);
          })
          this.isAttach = true;
        }
      }

      off() {
        this.isAttach = false;
        this.inputController.off('insertText')
        this.inputController.off('deleteLeft')
      }
    }
  1. 在应用界面布局中引入该控件即可,此处假设使用界面为Index.ets和控件CustomInput.ets在同一目录下。
ts 复制代码
    import { CustomInput } from './CustomInput'; // 导入控件

    @Entry
    @Component
    struct Index {

      build() {
        Column() {
          CustomInput() // 使用控件
        }
      }
    }

示例效果图

本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
bestadc1 小时前
入门版 鸿蒙 组件导航 (Navigation)
harmonyos
HarmonyOS_SDK3 小时前
几行代码配置高频按钮,保障用户体验一致
harmonyos
不脱发的程序猿7 小时前
嵌入式设备异常掉电怎么办?
嵌入式
邹荣乐8 小时前
鸿蒙HarmonyOS开发:多种内置弹窗及自定义弹窗的详细使用指南
harmonyos
无聊到发博客的菜鸟11 小时前
STM32实现SPI转USB虚拟串口输出(实测40M时钟不丢包)
stm32·嵌入式·usb·spi·虚拟串口
别说我什么都不会11 小时前
【仓颉三方库】图像处理库 —— gifdrawable4cj
harmonyos
彭不懂赶紧问11 小时前
鸿蒙NEXT开发浅进阶到精通04:类似支付宝横向导航栏与list组件联动随航
前端·harmonyos
HarmonyOS小助手11 小时前
如何用DevEco Studio的ArkUI Inspector轻松搞定鸿蒙应用UI布局
harmonyos·鸿蒙·deveco studio·harmonyos next·arkui inspector
hellojackjiang201113 小时前
全平台开源即时通讯IM框架MobileIMSDK:7端+TCP/UDP/WebSocket协议,鸿蒙NEXT端已发布,5.7K Stars
网络·harmonyos·即时通讯·im开发
特立独行的猫a19 小时前
HarmonyOS NEXT 诗词元服务项目开发上架全流程实战(一、项目介绍及实现效果)
华为·harmonyos·元服务·上架