类型约束 (Selector.type)
Selector.type
声明
node.selector():type(val)
参数
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| val | string | 是 | 控件类型,如 TextView、ListView 等,支持正则表达式 |
返回值
| 类型 | 说明 |
|---|---|
| Selector | 返回控件查找器,用于链式编程 |
说明
通过控件的类型(className)约束控件。
常见控件类型:
- 文本类:
TextView、EditText、Button - 图片类:
ImageView、ImageButton - 布局类:
LinearLayout、FrameLayout、RelativeLayout、ConstraintLayout - 列表类:
ListView、RecyclerView、GridView、ScrollView - 输入类:
EditText、CheckBox、RadioButton、Switch、SeekBar - 容器类:
ViewPager、TabLayout、WebView
匹配规则:
- 支持正则表达式匹配
- 可以只写类名不写完整包路径:
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()