HarmonyOS TextInput不设置TYPE时怎么弹出数字键盘或者number类型时能输入负数

HarmonyOS
2024-12-25 13:40:50
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

参考以下示例,弹出数字键盘可以输入“-”:

// xxx.ets
import { inputMethod } from '@kit.IMEKit'
import { BusinessError } from '@ohos.base';

@Entry
@Component
struct TextInputExample {
  @State text: string = ''
  @State positionInfo: CaretOffset = { index: 0, x: 0, y: 0 }
  @State passwordState: boolean = false
  controller: TextInputController = new TextInputController()
  private inputController: inputMethod.InputMethodController = inputMethod.getController();
  private keyboardStatus: number = 0; // 默认是none, 1是hide,2是show
  @State codeTxt: string = '';
  private isAttached: boolean = false;

  build() {
    Column() {
      TextInput({ text: this.codeTxt, controller: this.controller })
        .width(300)
        .focusable(false)
        .onClick(async () => {
          if (!this.isAttached) {
            await this.attachAndListener();
          }

          console.info('IsaKit keyboardStatus =' + this.keyboardStatus)
          // if (this.isAttached && this.keyboardStatus != 2) {
          // 输入法配置项
          let textConfig: inputMethod.TextConfig = {
            inputAttribute: {
              textInputType: inputMethod.TextInputType.NUMBER,
              enterKeyType: inputMethod.EnterKeyType.GO
            }
          };
          // 控件绑定输入法
          await this.inputController.attach(true, textConfig)
          return
          // }
        })
    }
  }

  // 解绑输入法
  detach() {
    this.inputController.off('insertText');
    this.inputController.off('deleteLeft');
    this.inputController.off('sendKeyboardStatus');
    this.inputController.detach((err: BusinessError) => {
      if (err) {
        console.error(`Failed to detach: ${JSON.stringify(err)}`);
        return;
      }
      this.isAttached = false
      console.log('Succeeded in detaching inputMethod.');
    });
  }

  // 绑定和设置监听
  async attachAndListener() {
    this.isAttached = true;
    // 输入法配置项
    let textConfig: inputMethod.TextConfig = {
      inputAttribute: {
        textInputType: inputMethod.TextInputType.NUMBER,
        enterKeyType: inputMethod.EnterKeyType.GO
      }
    };

    // 控件绑定输入法
    await this.inputController.attach(true, textConfig)
    // this.isAttached = true
    this.attachListener()
  }

  /*
  * 订阅输入法回调
  */
  attachListener(): void {
    this.inputController.on('insertText', (text) => {

      this.codeTxt += text;
      console.info('this.inputText', 'insertText this.inputText===' + this.codeTxt)
    })
    // 订阅输入法应用向左删除事件
    this.inputController.on('deleteLeft', (length) => {
      this.codeTxt = this.codeTxt.substring(0, this.codeTxt.length - 1);
      console.info('this.inputText', 'deleteLeft this.inputText===' + this.codeTxt, 'length' + length)
    })
    // 订阅输入法应用发送输入法软键盘状态事件
    this.inputController.on('sendKeyboardStatus', (keyboardStatus: inputMethod.KeyboardStatus) => {
      this.keyboardStatus = keyboardStatus
      console.info('IsaKit this.inputText keyboardStatus= ' + this.keyboardStatus)
    })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
分享
微博
QQ
微信
回复
2024-12-25 16:31:23
相关问题
HarmonyOS textInput显示数字键盘
690浏览 • 1回复 待解决
HarmonyOS 键盘输入不能输入负数
534浏览 • 1回复 待解决
HarmonyOS 怎么监控键盘弹出或者收起
591浏览 • 1回复 待解决
HarmonyOS textinput键盘弹出问题
1394浏览 • 1回复 待解决
HarmonyOS TextInput如何主动弹出键盘
1135浏览 • 1回复 待解决
TextInput输入行满无法自动换行
1252浏览 • 1回复 待解决
HarmonyOS 弹出键盘,web页面白屏
829浏览 • 1回复 待解决
键盘弹出,页面的自适应
2310浏览 • 1回复 待解决