获取兄弟控件 (Selector.brother)
Selector.brother
声明
node.selector():brother()
node.selector():brother(val)
node.selector():brother(val1, val2, ...)
参数
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| val | number | 否 | 获取第 val 个兄弟控件。默认获取所有兄弟。(1)=第1个兄弟,(1,2)=第1和第2个兄弟,(1.4)=1-4之间所有兄弟,(0.1)=下一个兄弟,(-0.1)=上一个兄弟,(-1)=倒数第1个兄弟 |
返回值
| 类型 | 说明 |
|---|---|
| Selector | 返回控件查找器,用于链式编程 |
说明
添加兄弟控件约束,用于在查找后获取匹配控件的同级控件。
工作原理:
- 先根据其他约束找到目标控件
- 然后返回与目标控件同一父控件下的其他控件
- 支持灵活的参数格式获取不同位置的兄弟控件
参数格式详解:
- 无参数
brother()- 获取所有兄弟控件(不含自己) brother(1)- 获取第1个兄弟控件brother(-1)- 获取最后1个兄弟控件brother(0.1)- 获取下一个兄弟(相对于当前控件)brother(-0.1)- 获取上一个兄弟(相对于当前控件)brother(1, 2)- 获取第1和第2个兄弟brother(1.4)- 获取第1到第4个兄弟(范围)
适用场景:
- 从已知控件导航到相邻控件
- 获取列表项中同一行的其他元素
- 通过一个特征明显的控件定位其兄弟控件
特殊语法说明:
0.1表示当前控件后面紧邻的下一个兄弟-0.1表示当前控件前面紧邻的上一个兄弟- 这是相对定位,不同于绝对索引
与 Node.brother() 的区别:
selector:brother()- 是选择器的约束方法,用于查找时n:brother()- 是控件对象的方法,对已找到的控件操作
待实现
该接口正在开发中。
示例
local node = require("node")
-- 获取所有兄弟
local nodes = node.selector():desc(".*充电.*"):brother():find_all()
-- 获取第一个兄弟控件
local nodes = node.selector():desc(".*充电.*"):brother(1):find_all()
-- 获取第1和第2个兄弟控件
local nodes = node.selector():desc(".*充电.*"):brother(1, 2):find_all()
-- 获取1-4之间的所有兄弟控件
local nodes = node.selector():desc(".*充电.*"):brother(1.4):find_all()
-- 获取当前控件的下一个兄弟控件
local nodes = node.selector():desc(".*充电.*"):brother(0.1):find_all()
-- 获取当前控件的上一个兄弟控件
local nodes = node.selector():desc(".*充电.*"):brother(-0.1):find_all()
-- 获取倒数第1个兄弟控件
local nodes = node.selector():desc(".*充电.*"):brother(-1):find_all()
if nodes then
for _, node in ipairs(nodes) do
print(node.desc)
end
end
AScript 对应
# AScript (Python)
from ascript.android.node import Selector
nodes = Selector().desc(".*充电.*").brother().find_all()
nodes = Selector().desc(".*充电.*").brother(0.1).find_all() # 下一个兄弟
nodes = Selector().desc(".*充电.*").brother(-0.1).find_all() # 上一个兄弟