HarmonyOS 鸿蒙Next界面中多个textInput,如何指定某一个主动获取焦点,非首次进入情况

HarmonyOS 鸿蒙Next界面中多个textInput,如何指定某一个主动获取焦点,非首次进入情况

界面中多个textInput,如何指定某一个主动获取焦点,非首次进入情况

2 回复

可以通过 focusControl.requestFocus指定组件获取焦点,以下为代码示例:

import promptAction from '[@ohos](/user/ohos).promptAction';

[@Entry](/user/Entry)

[@Component](/user/Component)

struct RequestFocusExample {

  [@State](/user/State) selectId: string = 'A'

  build() {

    Column({ space:20 }){

      Row({space: 5}) {

        TextInput({placeholder:'请输入1。。。'})

          .width(80).height(70).fontColor(Color.White)

          .key('A')

        TextInput({placeholder:'请输入2。。。'})

          .width(80).height(70).fontColor(Color.White)

          .key('B')

        TextInput({placeholder:'请输入3。。。'})

          .width(80).height(70).fontColor(Color.White)

          .key('C')

        TextInput({placeholder:'请输入4。。。'})

          .width(80).height(70).fontColor(Color.White)

          .key('D')

      }

      Row({space: 5}) {

        Button("A-RequestFocus")

          .width(80).height(70).fontColor(Color.White)

          .onClick(() => {

            let res = focusControl.requestFocus('A')      // 使选中的this.selectId的组件获焦

            if (res) {

              promptAction.showToast({message: 'Request success'})

            } else {

              promptAction.showToast({message: 'Request failed'})

            }

          })

        Button("B-RequestFocus")

          .width(80).height(70).fontColor(Color.White)

          .onClick(() => {

            let res = focusControl.requestFocus('B')      // 使选中的this.selectId的组件获焦

            if (res) {

              promptAction.showToast({message: 'Request success'})

            } else {

              promptAction.showToast({message: 'Request failed'})

            }

          })

        Button("C-RequestFocus")

          .width(80).height(70).fontColor(Color.White)

          .onClick(() => {

            let res = focusControl.requestFocus('C')      // 使选中的this.selectId的组件获焦

            if (res) {

              promptAction.showToast({message: 'Request success'})

            } else {

              promptAction.showToast({message: 'Request failed'})

            }

          })

        Button("DS-RequestFocus")

          .width(80).height(70).fontColor(Color.White)

          .onClick(() => {

            let res = focusControl.requestFocus('D')      // 使选中的this.selectId的组件获焦

            if (res) {

              promptAction.showToast({message: 'Request success'})

            } else {

              promptAction.showToast({message: 'Request failed'})

            }

          })

      }

    }.width('100%').margin({ top:20 })

  }

}

更多关于HarmonyOS 鸿蒙Next界面中多个textInput,如何指定某一个主动获取焦点,非首次进入情况的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next界面中,若要在非首次进入的情况下指定某一个TextInput主动获取焦点,可以采取以下措施:

  1. 编程控制焦点

    • 在页面的适当事件处理函数中(如onShow或其他自定义事件),通过调用focusControl.requestFocus方法,并传入目标TextInput的标识(如ID或key),显式请求焦点。
  2. 确保TextInput可获取焦点

    • 检查目标TextInput的focusable属性是否设置为true,确保该组件可接受焦点。
  3. 避免干扰

    • 确保没有其他组件或逻辑(如焦点策略、输入法设置等)干扰焦点的获取。

示例代码如下:

@Entry
@Component
struct TextInputExample {
    // ...其他代码
    build() {
        // ...其他布局
        TextInput({ /* ...属性 */ }).key('targetKey') // 为目标TextInput设置key
        // ...其他布局
        .onClick(() => {
            focusControl.requestFocus('targetKey'); // 请求焦点
        })
    }
}

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部