HarmonyOS鸿蒙Next中当前页面弹出软键盘时,显示自定义弹窗后,会导致软键盘被收起

HarmonyOS鸿蒙Next中当前页面弹出软键盘时,显示自定义弹窗后,会导致软键盘被收起 当前页面弹出软键盘时,显示自定义弹窗后,会导致软键盘被收起,期望是显示弹窗时,软键盘不收起:

4 回复

将弹窗的所有组件都设置上.focusable(false),主要是最外层的组件设置上.focusable(false)

更多关于HarmonyOS鸿蒙Next中当前页面弹出软键盘时,显示自定义弹窗后,会导致软键盘被收起的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


楼主你好

import { inputMethod } from '@kit.IMEKit'
import { BusinessError } from '@kit.BasicServicesKit'


@CustomDialog
export struct TestDialogController {
  private controller: CustomDialogController

  build() {
    Text('测试信息')
  }
}


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

  async detach() {
    await this.inputController.detach();
    console.log('Succeeded in detaching inputMethod.');
  }

  async aboutToAppear(): Promise<void> {
    await this.attachAndListener();
  }

  build() {
    Column() {

      Button('弹窗显示2秒后关闭').onClick((event: ClickEvent) => {
        const dialog = new CustomDialogController({
          builder: TestDialogController(),
          customStyle: true,
          autoCancel: false,
          isModal: true,
          onWillDismiss: () => {
          }
        })
        dialog.open()
        setTimeout(() => {
          dialog.close()
        }, 2000)
      })


      TextInput({ text: this.codeTxt, placeholder: '内容' })
        .width(300)
        .focusable(false)
        .onClick(async () => {
          this.inputController.showTextInput((err: BusinessError) => {
            if (err) {
              console.error(`Failed to showTextInput: ${JSON.stringify(err)}`);
              return;
            }
            console.log('Succeeded in showing the inputMethod.');
          });
        })
    }
  }

  // 绑定和设置监听
  async attachAndListener() {
    // 输入法配置项
    let textConfig: inputMethod.TextConfig = {
      inputAttribute: {
        textInputType: inputMethod.TextInputType.TEXT,
        enterKeyType: inputMethod.EnterKeyType.GO
      }
    };
    // 控件绑定输入法
    await this.inputController.attach(false, 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', async (keyboardStatus: inputMethod.KeyboardStatus) => {
      this.keyboardStatus = keyboardStatus
      console.info('IsaKit this.inputText keyboardStatus= ', this.keyboardStatus)
    })
  }
}

在HarmonyOS鸿蒙Next中,当当前页面弹出软键盘时,如果同时显示自定义弹窗,会导致软键盘被收起。这是因为系统在处理软键盘和弹窗的显示逻辑时,默认会将焦点从输入框转移到弹窗上,从而导致软键盘自动收起。

要解决这个问题,可以在显示自定义弹窗时,通过调用InputMethodManagershowSoftInput方法,手动保持软键盘的显示状态。具体实现可以参考以下代码:

import inputMethod from '@ohos.inputmethod';

// 显示自定义弹窗时保持软键盘显示
function showCustomDialog() {
    // 显示自定义弹窗的代码
    ...

    // 保持软键盘显示
    inputMethod.getInputMethodAbility().then((ability) => {
        ability.showSoftInput();
    });
}

在HarmonyOS鸿蒙Next中,当页面弹出软键盘时,如果同时显示自定义弹窗,可能会导致软键盘被收起。这是因为系统默认在弹出新窗口时,会将焦点转移到新窗口,从而导致软键盘失去焦点并收起。

解决方法:

  1. 保持焦点:在显示自定义弹窗时,确保输入框保持焦点,避免软键盘被收起。
  2. 调整弹窗布局:可以通过调整弹窗的布局,使其不影响输入框的焦点状态。
  3. 监听键盘事件:监听软键盘的显示和隐藏事件,在显示弹窗时手动保持软键盘的显示状态。

通过这些方法,可以有效避免软键盘被意外收起的问题。

回到顶部