如何获取 text 里面的时间数据,这个数据是动态的,获取的id如下: 例如: id("0_resource_name_obfuscated").className("android.widget.TextView").text("06:32").findOne()
我需要获取到这个控件里面的 text 显示的内容,正确的应该是 "06:32"
界面中有两个相同 id 的 控件, 控件差异是 indexInParent 不一样,一个是 1 ,一个是 0 ,应该如何获取 indexInParent = 1 的控件
通过 .indexInParent(1) 获取不了
回答
要获取动态时间数据,你不能在选择器中使用具体的text值(如"06:32"),因为这是变化的。正确的做法是先通过其他稳定属性定位控件,然后再获取其text内容。
文档地址 https://docs.hamibot.com/
// 通过id和className定位控件,不指定text值
var timeControl = id("0_resource_name_obfuscated").className("android.widget.TextView").findOne();
// 获取控件的text内容
var timeText = timeControl.text();
// 打印获取到的时间
console.log("当前时间: " + timeText);
// 或者用toast显示
toast("获取到的时间: " + timeText);
关键点说明:
- 在选择器中不要包含
.text("06:32"),因为这个值是动态变化的,会导致选择器无法匹配 - 先用稳定的属性(如id和className)找到控件
- 然后使用
text()方法获取该控件的文本内容
如果这个id在不同应用版本中会变化(根据知识库[4],某些应用如微信、网易云音乐的id会随版本更新而变化),建议考虑使用其他更稳定的定位方式,比如:
- 使用desc(描述)属性(如果有的话)
- 使用bounds(控件位置)进行定位
- 或者通过父控件关系定位(使用parent()方法)
获取到的时间文本可以直接用于后续操作,如时间比较、格式化等。
补充回答 1
文档地址 https://docs.hamibot.com/
// 获取indexInParent为1的控件
var targetControl = id("0_resource_name_obfuscated")
.className("android.widget.TextView")
.indexInParent(1)
.findOne();
// 获取并显示控件文本
if (targetControl) {
var textContent = targetControl.text();
console.log("获取到的文本: " + textContent);
toast("获取到的文本: " + textContent);
} else {
console.log("未找到指定控件");
toast("未找到指定控件");
}
关键说明:
- 使用
.indexInParent(1)来指定要获取父容器中索引为1的控件(索引从0开始计数) - 确保选择器中其他属性(如id和className)设置正确,以精确定位到目标父容器下的控件
findOne()方法会返回第一个匹配的控件,配合indexInParent使用可以精确定位到特定位置的控件
如果控件层级比较复杂,还可以结合parent()方法先定位到共同的父容器,再获取子控件:
// 先定位到共同的父容器
var parent = id("parent_id").findOne();
// 在父容器中查找indexInParent为1的子控件
var targetControl = parent.findOne(id("0_resource_name_obfuscated").indexInParent(1));
补充回答 2
文档地址 https://docs.hamibot.com/
// 方案1:使用child()方法(更可靠)
var parent = id("parent_id").findOne(); // 先获取共同父容器
if (parent) {
var targetControl = parent.child(1); // 获取父容器中索引为1的子控件
var textContent = targetControl.text();
toast("获取到的文本: " + textContent);
}
// 方案2:如果不知道父容器id,可以这样
var controls = id("0_resource_name_obfuscated").className("android.widget.TextView").find();
if (controls.size() > 1) {
var targetControl = controls[1]; // 获取第二个匹配的控件(索引1)
var textContent = targetControl.text();
toast("获取到的文本: " + textContent);
}
// 方案3:使用bounds位置信息区分
var allControls = id("0_resource_name_obfuscated").className("android.widget.TextView").find();
var targetControl = null;
for (var i = 0; i < allControls.size(); i++) {
var control = allControls[i];
if (control.indexInParent() === 1) { // 这里检查实际的indexInParent值
targetControl = control;
break;
}
}
if (targetControl) {
toast("获取到的文本: " + targetControl.text());
}
// 调试方法:打印控件树信息
function dumpControlInfo() {
var controls = id("0_resource_name_obfuscated").className("android.widget.TextView").find();
for (var i = 0; i < controls.size(); i++) {
var c = controls[i];
console.log("控件 " + i + ": indexInParent=" + c.indexInParent() +
", text=" + c.text() +
", bounds=" + c.bounds().toString());
}
}
// 调用此函数查看实际的控件信息
dumpControlInfo();
.indexInParent()选择器有时可能不起作用的原因:
- 实际的indexInParent值可能与你看到的不一致(可能包含不可见控件)
- 控件树可能在查找过程中发生了变化
- 选择器执行时,控件可能还未完全加载
建议先运行dumpControlInfo()函数查看实际的控件树结构,确认真正的indexInParent值。在实际应用中,使用child()方法从已知父容器中获取子控件通常比直接用indexInParent选择器更可靠。