跳到主要内容

描述约束 (Selector.desc)

Selector.desc

声明

node.selector():desc(description)
node.selector():desc_contains(text)
node.selector():desc_starts_with(text)
node.selector():desc_matches(regex)

参数

参数类型说明
descriptionstring描述内容

返回值

类型说明
Selector选择器对象

说明

通过控件的无障碍描述(content-description)约束控件。

什么是 content-description:

  • Android 无障碍服务使用的控件描述文本
  • 主要用于屏幕阅读器(如 TalkBack)朗读控件功能
  • 开发者为非文本控件(如图标按钮)设置的说明文字

方法变体:

  • desc(str) - 精确匹配
  • desc_contains(str) - 包含匹配
  • desc_starts_with(str) - 前缀匹配
  • desc_matches(regex) - 正则匹配

适用场景:

  • 图标按钮(如返回、分享、更多等)
  • 无文本的 ImageView、ImageButton
  • 自定义图形控件

典型应用:

  • 返回按钮:desc("返回")desc("Navigate up")
  • 菜单按钮:desc("更多选项")desc("More options")
  • 搜索图标:desc("搜索")

注意事项:

  • 并非所有控件都有 content-description
  • 描述内容可能随系统语言变化
  • 某些应用未设置无障碍描述,此时该属性为空
  • 与 text 属性互补使用,覆盖更多控件
待实现

该接口正在开发中。

示例

local node = require("node")

local n = node.selector():desc("返回"):find()
local n2 = node.selector():desc_contains("搜索"):find()

AScript 对应

# AScript (Python)
from ascript.android.node import Selector

node = Selector().desc("返回").find()