回答
可以通过 floaty
模块创建和控制悬浮窗(悬浮球)。以下是基本操作步骤和示例代码:
1. 创建悬浮窗
使用 floaty.window()
方法定义悬浮窗布局,例如:
// 文档地址 https://docs.hamibot.com/
var floatyWin = floaty.window(
<vertical padding="16">
<button id="startBtn" text="开始任务" bg="#4CAF50" />
<button id="stopBtn" text="停止任务" bg="#F44336" enabled="false" />
<button id="exitBtn" text="退出脚本" bg="#607D8B" />
</vertical>
);
2. 绑定按钮事件
通过 click()
方法监听按钮点击事件:
floatyWin.startBtn.click(function() {
// 执行开始逻辑
toast("任务开始");
ui.run(function() {
floatyWin.startBtn.enabled = false;
floatyWin.stopBtn.enabled = true;
});
});
floatyWin.stopBtn.click(function() {
// 执行停止逻辑
toast("任务停止");
ui.run(function() {
floatyWin.startBtn.enabled = true;
floatyWin.stopBtn.enabled = false;
});
});
floatyWin.exitBtn.click(function() {
floatyWin.close();
hamibot.exit();
});
3. 保持悬浮窗不被关闭
脚本停止时悬浮窗会自动关闭,需用 setInterval
保持运行:
setInterval(() => {}, 1000);
4. 其他功能
- 调整大小/位置:默认支持拖拽调整,可通过
setSize()
设置尺寸。 - 触摸权限:使用
window.setTouchable(true/false)
控制是否拦截触摸事件。
注意事项
- 确保已开启无障碍服务和悬浮窗权限。
- 所有 UI 操作需通过
ui.run()
在主线程执行。
ℹ️ 本回答由 AI 生成,内容仅供参考