跳到主要内容

类型约束 (Selector.type)

Selector.type

声明

node.selector():type(val)

参数

参数类型是否必填说明
valstring控件类型,如 TextView、ListView 等,支持正则表达式

返回值

类型说明
Selector返回控件查找器,用于链式编程

说明

通过控件的类型(className)约束控件。

常见控件类型:

  • 文本类TextViewEditTextButton
  • 图片类ImageViewImageButton
  • 布局类LinearLayoutFrameLayoutRelativeLayoutConstraintLayout
  • 列表类ListViewRecyclerViewGridViewScrollView
  • 输入类EditTextCheckBoxRadioButtonSwitchSeekBar
  • 容器类ViewPagerTabLayoutWebView

匹配规则:

  • 支持正则表达式匹配
  • 可以只写类名不写完整包路径:type("TextView") 等同于 type(".*TextView")
  • 匹配自定义控件:type(".*CustomButton")

适用场景:

  • 配合其他约束缩小查找范围
  • 查找特定类型的所有控件(如所有输入框)
  • 区分相同文本但不同类型的控件

注意事项:

  • 仅通过 type 约束可能匹配到大量控件,建议配合其他约束使用
  • 某些自定义控件的类名可能较长或不规范
  • 继承关系不会自动匹配(如 Button 不会匹配 ImageButton)
待实现

该接口正在开发中。

示例

local node = require("node")

-- 查找所有类型为 TextView 的控件
local nodes = node.selector():type("TextView"):find_all()

if nodes then
for _, node in ipairs(nodes) do
print(node)
end
else
print("没有找到任何控件")
end

AScript 对应

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

nodes = Selector().type("TextView").find_all()