"ui";
ui.run(function () {
// 创建悬浮窗
var floatyWin = floaty.window(
<vertical padding="5" backgroundColor="#33000000" gravity="center">
<horizontal spacing="0">
<button id="startBtn" text="开始" width="70" height="60" textSize="12sp" margin="0" padding="2"/>
<button id="exitBtn" text="停止" width="70" height="60" textSize="12sp" margin="0" padding="2"/>
</horizontal>
</vertical>
);
// 设置悬浮窗位置
floatyWin.setPosition(device.width - 420, 100);
// 定义循环标志
var running = false;
// 开始按钮点击事件
floatyWin.startBtn.click(function () {
if (running) return;
running = true;
log("开始运行");
// 启动子线程执行耗时任务
threads.start(function () {
while (running) {
// 示例操作:模拟脚本行为
log("正在运行中...");
sleep(1000); // 每秒执行一次
}
log("任务结束");
});
});
// 停止按钮点击事件
floatyWin.exitBtn.click(function () {
running = false;
log("停止运行");
floatyWin.close();
});
}); 这段代码 改成用ui.layout显示
"ui";模式和 floaty.window 模式是冲突的,那我既需要悬浮窗显示功能按钮,同时也需要"ui";模式显示配置模式下的参数 该怎么写
回答
文档地址 https://docs.hamibot.com/
要将你的代码从 floaty.window
转换为使用 ui.layout
来创建界面,你需要理解两者的主要区别:floaty.window
创建的是一个悬浮窗,而 ui.layout
是用于设置脚本运行时的主界面(通常是全屏或占据整个活动窗口)。
以下是将你提供的代码改写为使用 ui.layout
的示例:
// 文档地址 https://docs.hamibot.com/
ui.layout(
<vertical padding="5" backgroundColor="#33000000" gravity="center">
<horizontal spacing="0">
<button id="startBtn" text="开始" width="70" height="60" textSize="12sp" margin="0" padding="2"/>
<button id="exitBtn" text="停止" width="70" height="60" textSize="12sp" margin="0" padding="2"/>
</horizontal>
</vertical>
);
// 定义循环标志
var running = false;
// 开始按钮点击事件
startBtn.click(function () {
if (running) return;
running = true;
log("开始运行");
// 启动子线程执行耗时任务
threads.start(function () {
while (running) {
// 示例操作:模拟脚本行为
log("正在运行中...");
sleep(1000); // 每秒执行一次
}
log("任务结束");
});
});
// 停止按钮点击事件
exitBtn.click(function () {
running = false;
log("停止运行");
hamibot.exit(); // 退出脚本
});
注意事项:
-
ui.layout
不是悬浮窗:- 使用
ui.layout
会创建一个完整的 UI 界面,通常作为脚本的主界面。 - 如果你希望保持悬浮窗的行为(例如在其他应用之上显示),应该继续使用
floaty.window()
。
- 使用
-
ID 引用方式不同:
- 在
floaty.window()
中,通过floatyWin.idName
获取控件。 - 在
ui.layout()
中,直接使用idName
(如startBtn
)即可访问控件。
- 在
-
关闭与退出:
floaty.window().close()
用于关闭悬浮窗。ui.layout
的界面关闭通常通过hamibot.exit()
来实现脚本退出。
如果你确实需要保留悬浮窗功能,请继续使用 floaty.window()
。如果只是想将界面嵌入到脚本的主 UI 中,那么上面的 ui.layout
改写就足够了。
补充回答 1
文档地址 https://docs.hamibot.com/
在 Hamibot 中,"ui"
模式(即主界面)和 floaty.window()
悬浮窗确实不能同时共存。如果你需要同时实现 悬浮窗显示功能按钮 和 配置界面显示参数设置等内容,你可以通过以下方式来实现:
✅ 推荐方案:使用两个独立的脚本
-
脚本 A(配置界面):
使用ui.layout
创建一个带配置项的界面,用于设置参数、保存数据等。 -
脚本 B(悬浮窗控制):
使用floaty.window()
创建悬浮窗按钮,并读取脚本 A 存储的配置参数运行任务。
脚本 A:配置界面(config.js)
// 文档地址 https://docs.hamibot.com/
var config = {
interval: 1000,
enabled: true
};
// 使用存储保存配置
storage.put("config", config);
ui.layout(
<vertical padding="10">
<text text="设置间隔时间(毫秒):" />
<input id="intervalInput" text="{{config.interval}}" />
<button id="saveBtn" text="保存配置" />
</vertical>
);
saveBtn.click(function () {
var interval = parseInt(intervalInput.text());
if (isNaN(interval)) interval = 1000;
storage.put("config", {
interval: interval,
enabled: true
});
toast("配置已保存");
});
脚本 B:悬浮窗控制(main.js)
// 文档地址 https://docs.hamibot.com/
var config = storage.get("config") || {
interval: 1000
};
var floatyWin = floaty.window(
<vertical padding="5" backgroundColor="#33000000" gravity="center">
<horizontal spacing="0">
<button id="startBtn" text="开始" width="70" height="60" textSize="12sp" margin="0" padding="2"/>
<button id="exitBtn" text="停止" width="70" height="60" textSize="12sp" margin="0" padding="2"/>
</horizontal>
</vertical>
);
floatyWin.setPosition(device.width - 420, 100);
var running = false;
floatyWin.startBtn.click(function () {
if (running) return;
running = true;
threads.start(function () {
while (running) {
log("执行任务,间隔:" + config.interval + "ms");
sleep(config.interval);
}
log("任务结束");
});
});
floatyWin.exitBtn.click(function () {
running = false;
floatyWin.close();
});
✅ 替代方案:用两个悬浮窗模拟“主界面+控制台”
你也可以完全放弃 "ui"
模式,使用两个不同的 floaty.window()
实现类似效果:
- 一个悬浮窗作为“配置界面”
- 另一个作为“控制面板”
但这种方式交互体验略差,推荐还是使用两个脚本来实现更清晰的分工。
📌 总结
方案 | 特点 | 适用场景 |
---|---|---|
脚本 A(ui)+ 脚本 B(floaty) | 配置清晰、结构分明 | 复杂参数 + 控制逻辑 |
两个 floaty 窗口 | 不依赖主界面 | 快速原型或简单需求 |
如需进一步帮助如何设计具体逻辑,请继续提问。