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

HarmonyOS
1天前
浏览
收藏 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)
    })
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 键盘输入不能输入负数
35浏览 • 1回复 待解决
HarmonyOS textinput键盘弹出问题
51浏览 • 1回复 待解决
HarmonyOS TextInput如何主动弹出键盘
392浏览 • 1回复 待解决
HarmonyOS 弹出键盘,web页面白屏
29浏览 • 1回复 待解决
TextInput输入行满无法自动换行
465浏览 • 1回复 待解决
键盘弹出,页面的自适应
1616浏览 • 1回复 待解决