查找所有 (Selector.find_all)
Selector.find_all
声明
nodes = node.selector():find_all()
返回值
| 类型 | 说明 |
|---|---|
| table | 控件对象数组 |
说明
执行查找操作,返回所有匹配的控件。
工作原理:
- 根据之前设置的所有约束条件,在当前界面的控件树中查找
- 返回所有满足约束条件的控件组成的数组
- 如果没有匹配的控件,返回空数组或 nil
与 find() 的区别:
find()- 返回第一个匹配的单个 Nodefind_all()- 返回所有匹配的 Node 数组
适用场景:
- 需要操作所有匹配控件时(如批量点击)
- 统计匹配控件数量
- 遍历处理同类型的多个控件
性能考虑:
- find_all() 会遍历整个控件树,性能低于 find()
- 如果只需要第一个匹配项,优先使用 find()
- 大量控件时,建议先用其他约束缩小范围
注意事项:
- 返回的数组可以用
#nodes获取数量 - 使用
ipairs(nodes)或for i, n in ipairs(nodes)遍历 - 控件顺序按控件树的深度优先顺序排列
待实现
该接口正在开发中。
示例
local node = require("node")
local nodes = node.selector():type("android.widget.TextView"):find_all()
for _, node in ipairs(nodes) do
print(node:text())
end
AScript 对应
# AScript (Python)
from ascript.android.node import Selector
nodes = Selector().className("android.widget.TextView").find_all() # AScript 使用 className
for node in nodes:
print(node.text)