回答
你可以通过 floaty.window
创建一个带有按钮的悬浮窗,并为按钮设置点击事件来启动脚本运行。以下是一个完整的示例代码,你可以将它保存并运行在 Hamibot 环境中:
// 文档地址 https://docs.hamibot.com/
// 定义处理逻辑
var handlers = {
running: false,
start: function () {
if (this.running) return;
this.running = true;
toast('任务开始');
// 执行任务代码
},
stop: function () {
if (!this.running) return;
this.running = false;
toast('任务停止');
// 停止任务代码
},
};
// 创建悬浮窗布局
var floatyWin = floaty.window(
<vertical padding="16">
<button id="startBtn" text="开始任务" bg="#4CAF50" />
<button id="stopBtn" text="停止任务" bg="#F44336" enabled="false" />
</vertical>
);
// 设置“开始任务”按钮点击事件
floatyWin.startBtn.click(function () {
handlers.start();
ui.run(function () {
floatyWin.startBtn.enabled = false;
floatyWin.stopBtn.enabled = true;
});
});
// 设置“停止任务”按钮点击事件
floatyWin.stopBtn.click(function () {
handlers.stop();
ui.run(function () {
floatyWin.startBtn.enabled = true;
floatyWin.stopBtn.enabled = false;
});
});
// 保持悬浮窗不被关闭
setInterval(function () {}, 1000);
功能说明:
- 悬浮窗按钮:悬浮窗包含两个按钮,“开始任务”和“停止任务”。
- 点击“开始任务”按钮:会触发
handlers.start()
函数,显示提示信息“任务开始”,并禁用“开始任务”按钮,启用“停止任务”按钮。 - 点击“停止任务”按钮:会触发
handlers.stop()
函数,显示提示信息“任务停止”,并重新启用“开始任务”按钮,禁用“停止任务”按钮。
注意事项:
- 确保你已经在 Hamibot 中启用了无障碍服务,否则无法正常创建和操作悬浮窗。
- 如果你需要添加更多的功能,可以参考 UI 模块 的文档来扩展界面设计和交互逻辑。
ℹ️ 本回答由 AI 生成,内容仅供参考