@IT·互联网鸿蒙(HarmonyOS)开发知识

鸿蒙开发实战案例:H5页面调用自定义输入法案例思路

2025-04-01  本文已影响0人  迪士尼在逃程序员

介绍

本示例介绍了@ohos.web.webview组件和Web以及CustomDialog接口实现H5页面调用自定义输入法的功能。该场景多用于浏览器需要使用安全输入法时。

效果图预览

使用说明

实现步骤

实现H5页面调用自定义输入法,有两个关键点,一是需要将arkTS方法注册到h5页面中;二是要实现弹出键盘的组件。

  1. 构建一个 Browser 对象,集成浏览器的方法。创建一个自定义组件 TabletTitle,构成浏览器的工具栏。
// 自定义浏览器对象
export class Browser {
  inputValue: string = "";
  progress: number = 0;
  isRegistered: boolean = false;
  hideProgress: boolean = true;
  tabsController: TabsController = new TabsController();
  webController: WebviewController = new web_webview.WebviewController();
  // 跳转页面
  loadUrl(addr: string | Resource) {
    this.webController.loadUrl(addr);
  }
  // 返回页面
  back(): boolean {
    if (this.webController.accessBackward()) {
      this.webController.backward();
      return true;
    }
    return false;
  }
  // 前进页面
  forward() {
    if (this.webController.accessForward()) {
      this.webController.forward();
    }
  }
  // 刷新页面
  refresh() {
    this.webController.refresh();
  }
}
  1. 自定义键盘传入js对象 WebKeyboardObj, 构建两个函数:点击登录按钮事件和输入法弹窗弹出事件。其中输入法弹出事件中使用CustomDialog修饰的组件,打开自定义弹窗。
dialogController: CustomDialogController | null = new CustomDialogController({
  builder: CustomKeyboard({
    dialogClose: this.dialogClose,
    items: this.items,
    inputValue: this.inputValue,
    curKeyboardType: this.curKeyboardType,
    onKeyboardEvent: this.onKeyboardEvent,
    closeDialog: this.closeDialog
  }),
  isModal: false,
  alignment: DialogAlignment.Bottom,
  customStyle: true
});
// ...
webKeyboardObj: WebKeyboardObj = {
  // 点击登录按钮事件
  login: () => {
    promptAction.showToast({
      message: $r('app.string.custom_keyboard_to_h5_login_button'),
      duration: 2000
    });
    this.closeDialog();
  },
  // 输入法弹窗弹出事件
  openDialog: (value: string) => {
    this.dialogController?.open();
    this.dialogClose = false;
    if (value?.length) {
      this.inputValue = value;
    }
  }
}
  1. 将webKeyboardObj对象通过webController.registerJavaScriptProxy 注册到h5页面中,使页面中可以调用arkTS的方法。
// 注册方法到h5的js中
registerFunc(browser: Browser) {
  if (!browser.isRegistered) {
    browser.webController.registerJavaScriptProxy(this.webKeyboardObj, 'etsObj',
      ['login', 'openDialog'])
    browser.isRegistered = true;
    browser.webController.refresh();
  }
}
  1. 构建一个h5页面,在js层中调用注册进入的arkTS方法。
<script>
  function tapInput() {
    document.activeElement.blur();
    let input = document.getElementById("searchCon");
    etsObj.openDialog(input.value); // 打开自定义弹窗
  }
  function setInput(value) {
    let input = document.getElementById("searchCon");
    input.value = value;
  }
  function login() {
    etsObj.login(); // 点击登录按钮
  }
</script>

写在最后

上一篇 下一篇

猜你喜欢

热点阅读