【HarmonyOS NEXT】鸿蒙 如何在包含web组件的页面 让默认焦点有效

页面包含web组件Button组件等,把页面的默认焦点放到Button组件上,不起效果。

因为web组件默认会在组件加载完成后获取焦点;

可以在web的网页加载完成时onPageEnd回调中,将设置默认获焦的组件通过focusControl.requestFocus方法主动让焦点转移至参数指定的组件上

focusControl9+

焦点控制模块

requestFocus9+

requestFocus(value: string): boolean

方法语句中可使用的全局接口,调用此接口可以主动让焦点转移至参数指定的组件上。

参数:

名称 类型 必填 描述
value string 目标组件使用接口key(value: string)绑定的字符串。

返回值:

类型 说明
boolean 返回是否成功给目标组件申请到焦点。若参数指向的目标组件存在,且目标组件可获焦,则返回true,否则返回false。

说明

支持焦点控制的组件:TextInput、TextArea、Search、Button、Text、Image、List、Grid。焦点事件当前仅支持在真机上显示运行效果。

示例

javascript 复制代码
// requestFocus.ets
import promptAction from '@ohos.promptAction';

@Entry
@Component
struct RequestFocusExample {
  @State idList: string[] = ['A', 'B', 'C', 'D', 'E', 'F', 'LastPageId']
  @State selectId: string = 'LastPageId'

  build() {
    Column({ space:20 }){
      Row({space: 5}) {
        Button("id: " + this.idList[0] + " focusable(false)")
          .width(200).height(70).fontColor(Color.White)
          .key(this.idList[0])
          .focusable(false)
        Button("id: " + this.idList[1])
          .width(200).height(70).fontColor(Color.White)
          .key(this.idList[1])
      }
      Row({space: 5}) {
        Button("id: " + this.idList[2])
          .width(200).height(70).fontColor(Color.White)
          .key(this.idList[2])
        Button("id: " + this.idList[3])
          .width(200).height(70).fontColor(Color.White)
          .key(this.idList[3])
      }
      Row({space: 5}) {
        Button("id: " + this.idList[4])
          .width(200).height(70).fontColor(Color.White)
          .key(this.idList[4])
        Button("id: " + this.idList[5])
          .width(200).height(70).fontColor(Color.White)
          .key(this.idList[5])
      }
      Row({space: 5}) {
        Select([{value: this.idList[0]},
                {value: this.idList[1]},
                {value: this.idList[2]},
                {value: this.idList[3]},
                {value: this.idList[4]},
                {value: this.idList[5]},
                {value: this.idList[6]}])
          .value(this.selectId)
          .onSelect((index: number) => {
            this.selectId = this.idList[index]
          })
        Button("RequestFocus")
          .width(200).height(70).fontColor(Color.White)
          .onClick(() => {
            let res = focusControl.requestFocus(this.selectId)      // 使选中的this.selectId的组件获焦
            if (res) {
              promptAction.showToast({message: 'Request success'})
            } else {
              promptAction.showToast({message: 'Request failed'})
            }
          })
      }
    }.width('100%').margin({ top:20 })
  }
}

示意图:

按下TAB键,激活焦点态显示。

申请不存在的组件获焦:

申请不可获焦的组件获焦:

申请存在且可获焦的组件获焦:

相关推荐
河阿里8 分钟前
HTML5标准完全教学手册
前端·html·html5
吴声子夜歌9 分钟前
Vue3——新语法
前端·javascript·vue.js
jiayong2311 分钟前
第 36 课:任务详情抽屉快捷改状态
开发语言·前端·javascript·vue.js·学习
FFF_6345602317 分钟前
通用 vue 页面 js 下载任何文件的方法
开发语言·前端·javascript
光影少年28 分钟前
中级前端需要会的东西都有那些?
前端·学习·前端框架
琢磨先生TT36 分钟前
为什么很多后台系统功能不少,看起来却还是很廉价?
前端·vue.js·设计
ekuoleung36 分钟前
量化平台中的 DSL 设计与实现:从规则树到可执行策略
前端·后端
前端技术38 分钟前
通信网络基础(下篇):TCP/IP网络参考模型与传输层协议深度解析
华为
小研说技术43 分钟前
实时通信对比,一场MCP协议的技术革命
前端·后端·面试
kyriewen1 小时前
React Hooks原理:为什么不能写在if里?揭开Hook的“魔法”面纱
前端·react.js·前端框架