回答
你可以使用 Hamibot 的浮窗功能在设备桌面上创建开始和停止按钮。以下是一个简单的示例代码,用于创建一个带有开始和停止按钮的浮窗:
// 文档地址 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>
);
// 定义处理函数
var handlers = {
running: false,
start: function () {
if (this.running) return;
this.running = true;
toast('任务开始');
// 执行任务代码
},
stop: function () {
if (!this.running) return;
this.running = false;
toast('任务停止');
// 停止任务代码
}
};
// 按钮点击事件监听
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;
});
});
floatyWin.exitBtn.click(function () {
handlers.stop();
floatyWin.close();
hamibot.exit();
});
// 保持悬浮窗不被关闭
setInterval(function () {}, 1000);
这段代码会创建一个带有开始、停止和退出按钮的浮窗。当点击“开始”按钮时,会执行 start
方法,并禁用“开始”按钮,启用“停止”按钮。当点击“停止”按钮时,会执行 stop
方法,并重新启用“开始”按钮,禁用“停止”按钮。点击“退出脚本”按钮会关闭浮窗并退出脚本。
补充回答 1
你可以移除退出按钮,以下是修改后的代码:
// 文档地址 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" />
</vertical>
);
// 定义处理函数
var handlers = {
running: false,
start: function () {
if (this.running) return;
this.running = true;
toast('任务开始');
// 执行任务代码
},
stop: function () {
if (!this.running) return;
this.running = false;
toast('任务停止');
// 停止任务代码
}
};
// 按钮点击事件监听
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);
这段代码会创建一个带有开始和停止按钮的浮窗。当点击“开始”按钮时,会执行 start
方法,并禁用“开始”按钮,启用“停止”按钮。当点击“停止”按钮时,会执行 stop
方法,并重新启用“开始”按钮,禁用“停止”按钮。
ℹ️ 本回答由 AI 生成,内容仅供参考