目录

使用输入法进行文本输入时,输入框无响应

问题现象

在应用中用户尝试使用输入法进行文本输入时,输入框无法响应,导致无法输入任何内容或输入内容不符合预期。

背景知识

问题定位

  1. 对于输入法输入无效这个场景,主要原因可能是对应组件事件设置问题导致的。因此首先需要确认用于呈现输入内容的ArkUI组件。
    • TextInput/TextArea:单行文本/多行文本输入框组件:主要分析onChange事件和onSubmit事件或者其他事件设置是否合理。

      在密码模式下,设置showPassword属性时,在onSecurityStateChange回调中,建议增加状态同步。

      onWillInsert、onDidInsert、onWillDelete、onDidDelete回调仅支持系统输入法的场景。

      onWillChange的回调时序晚于onWillInsert、onWillDelete,早于onDidInsert、onDidDelete。

    • RichEditor:支持图文混排和文本交互式编辑的组件,检查onWillChange/onDidChange事件(添加图文变化前/后可触发的回调)或者aboutToIMEInput/onDidIMEInput事件(添加输入法输入内容前/后触发回调)设置是否正确。

      使用RichEditorStyledStringOptions构建的RichEditor组件不支持上述回调。

    • 如果上述ArkUI组件事件,问题也可能发生在自绘编辑框中,检查作为自绘编辑框的组件能力和状态变量设置是否正确。

      一般使用Text组件作为自绘编辑框的文本显示组件,状态变量作为Text组件要显示的内容。

  2. 除此之外,应用对于输入内容可能存在校验机制,导致输入字段被限制无法输入。
    • ArkUI组件:可以查看输入框组件是否设置输入框类型、正则表达过滤器和最大输入字符数限制等,参考具体组件的API。
    • Web组件:可以查看H5页面HTML的<input>标签,检查是否设置type和pattern。

分析结论

[h2]场景一

由于呈现输入内容的ArkUI组件对应事件设置不合理导致输入无效。

[h2]场景二

应用使用的是自绘编辑框用于呈现输入内容,由于ArkUI组件能力使用不当或者状态变量管理不正确导致输入无效。

[h2]场景三

应用对于输入内容有校验机制不合理。例如,作为输入框使用的ArkUI组件,未正确设置输入框类型、正则表达过滤器和最大输入字符数限制等,则可能导致无效输入无法被及时拦截。同样,在H5页面中,HTML的<input>标签若未合理设置type属性或pattern正则模式,也会造成输入验证失效,影响用户体验。

修改建议

[h2]场景一

修复或者完善对应事件逻辑,优化用户体验。其中以TextInput为例(其他请参考背景知识),添加事件:

TextInput({ text: 'Hello World', placeholder: 'input your word...', controller: this.controller })
  .type(InputType.Password)
  .showPassword(this.passwordState)
  .onChange((value: string) => {
    // 文本内容发生变化时触发该回调
    console.info('onChange is triggering: ', value);
  })
  .onSubmit((enterKey: EnterKeyType, event: SubmitEvent) => {
    // 按下输入法回车键时触发该回调
    console.info('onSubmit is triggering: ', enterKey, event.text);
  })
  .onTextSelectionChange((selectionStart: number, selectionEnd: number) => {
    // 文本选择的位置发生变化或编辑状态下光标位置发生变化时,触发该回调
    console.info('onTextSelectionChange is triggering: ', selectionStart, selectionEnd);
  })
  .onSecurityStateChange((isShowPassword: boolean) => {
    // 密码显隐状态切换时,触发该回调
    console.info('onSecurityStateChange is triggering: ', isShowPassword);
    this.passwordState = isShowPassword;
  })
  .onWillInsert((info: InsertValue) => {
    // 在将要输入时,触发该回调
    console.info('onWillInsert is triggering: ', info.insertValue, info.insertOffset);
    return true;
  })
  .onDidInsert((info: InsertValue) => {
    // 在输入完成时,触发该回调
    console.info('onDidInsert is triggering: ', info.insertValue, info.insertOffset);
  })
  .onWillDelete((info: DeleteValue) => {
    // 在将要删除时,触发该回调
    console.info('onWillDelete is triggering: ', info.deleteValue, info.deleteOffset);
    return true;
  })
  .onDidDelete((info: DeleteValue) => {
    // 在删除完成时,触发该回调
    console.info('onDidDelete is triggering: ', info.deleteValue, info.deleteOffset);
  })
  .onFocus(() => {
    // 绑定通用事件,输入框获焦时触发该回调
    console.info('onFocus is triggering');
  })

[h2]场景二

根据具体场景使用正确的组件能力和状态变量管理。以Text组件作为自绘编辑框的文本显示组件,状态变量inputText作为Text组件要显示的内容为例:

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');
    try {
      await this.inputController.attach(true, {
        inputAttribute: {
          textInputType: inputMethod.TextInputType.TEXT,
          enterKeyType: inputMethod.EnterKeyType.SEARCH
        }
      });
    } catch (error) {
      // TODO: Implement error handling.
    }
    if (!this.isAttach) {
      try {
        this.inputController.on('insertText', (text) => {
          this.inputText += text;
        });
      } catch (error) {
        // TODO: Implement error handling.
      }
      try {
        this.inputController.on('deleteLeft', (length) => {
          this.inputText = this.inputText.substring(0, this.inputText.length - length);
        });
      } catch (error) {
        // TODO: Implement error handling.
      }
      this.isAttach = true;
    }
  }

  off() {
    this.isAttach = false;
    this.inputController.off('insertText');
    this.inputController.off('deleteLeft');
  }
}

[h2]场景三

设置正确的校验机制,并增加提醒,引导用户合法输入。

完整示例:

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');
    try {
      await this.inputController.attach(true, {
        inputAttribute: {
          textInputType: inputMethod.TextInputType.TEXT,
          enterKeyType: inputMethod.EnterKeyType.SEARCH
        }
      });
    } catch (error) {
      // TODO: Implement error handling.
    }
    if (!this.isAttach) {
      try {
        this.inputController.on('insertText', (text) => {
          this.inputText += text;
        });
      } catch (error) {
        // TODO: Implement error handling.
      }
      try {
        this.inputController.on('deleteLeft', (length) => {
          this.inputText = this.inputText.substring(0, this.inputText.length - length);
        });
      } catch (error) {
        // TODO: Implement error handling.
      }
      this.isAttach = true;
    }
  }

  off() {
    this.isAttach = false;
    this.inputController.off('insertText');
    this.inputController.off('deleteLeft');
  }
}


@Entry
@Component
struct NavTitleSolution1 {
  controller: TextInputController = new TextInputController();
  @State passwordState: boolean = false;

  build() {
    Column() {
      TextInput({ text: 'Hello World', placeholder: 'input your word...', controller: this.controller })
        .type(InputType.Password)
        .showPassword(this.passwordState)
        .onChange((value: string) => {
          // 文本内容发生变化时触发该回调
          console.info('onChange is triggering: ', value);
        })
        .onSubmit((enterKey: EnterKeyType, event: SubmitEvent) => {
          // 按下输入法回车键时触发该回调
          console.info('onSubmit is triggering: ', enterKey, event.text);
        })
        .onTextSelectionChange((selectionStart: number, selectionEnd: number) => {
          // 文本选择的位置发生变化或编辑状态下光标位置发生变化时,触发该回调
          console.info('onTextSelectionChange is triggering: ', selectionStart, selectionEnd);
        })
        .onSecurityStateChange((isShowPassword: boolean) => {
          // 密码显隐状态切换时,触发该回调
          console.info('onSecurityStateChange is triggering: ', isShowPassword);
          this.passwordState = isShowPassword;
        })
        .onWillInsert((info: InsertValue) => {
          // 在将要输入时,触发该回调
          console.info('onWillInsert is triggering: ', info.insertValue, info.insertOffset);
          return true;
        })
        .onDidInsert((info: InsertValue) => {
          // 在输入完成时,触发该回调
          console.info('onDidInsert is triggering: ', info.insertValue, info.insertOffset);
        })
        .onWillDelete((info: DeleteValue) => {
          // 在将要删除时,触发该回调
          console.info('onWillDelete is triggering: ', info.deleteValue, info.deleteOffset);
          return true;
        })
        .onDidDelete((info: DeleteValue) => {
          // 在删除完成时,触发该回调
          console.info('onDidDelete is triggering: ', info.deleteValue, info.deleteOffset);
        })
        .onFocus(() => {
          // 绑定通用事件,输入框获焦时触发该回调
          console.info('onFocus is triggering');
        })
    }
    .justifyContent(FlexAlign.Center)
    .width('100%')
    .height('100%');

  }
}