// 创建更精简的悬浮窗
var floatyWin = floaty.window(
<vertical padding="8" bg="#D0F0C0">
<text text="脚本控制" fontSize="16" textColor="#0000FF" textStyle="bold" />
<horizontal gravity="center_vertical" marginTop="8">
  <text text="密码:" textSize="12" />
  <input id="pwdInput" text="" password="false" hint="支付密码" width="100dp" textSize="12" />
</horizontal>
<horizontal gravity="center_vertical" marginTop="8">
  <text text="数量:" textSize="12" />
  <button id="countToggleBtn" text="1" bg="#4CAF50" textColor="white" textSize="12" marginLeft="5" />
</horizontal>
<horizontal gravity="center" marginTop="8">
  <text id="timeDisplay" textSize="12" textColor="#FF0000" />
</horizontal>
<horizontal gravity="center" marginTop="8">
  <button id="startBtn" text="开始" bg="#4CAF50" textColor="white" textSize="12" marginRight="5" />
  <button id="closeBtn" text="关闭" bg="#F44336" textColor="white" textSize="12" marginLeft="5" />
</horizontal>
</vertical>
);
// 初始化变量
var selectedCount = 1;
var isRunning = true;
// 更新时间的函数 - 只显示时分秒毫秒
function updateTime() {
while (isRunning) {
ui.run(function() {
var now = new Date();
var timeStr = util.format("%02d:%02d:%02d.%03d",
now.getHours(), now.getMinutes(), now.getSeconds(), now.getMilliseconds());
floatyWin.timeDisplay.setText(timeStr);
});
sleep(50);
}
}
// 数量切换按钮点击事件
floatyWin.countToggleBtn.click(() => {
selectedCount = selectedCount === 1 ? 2 : 1;
floatyWin.countToggleBtn.setText(selectedCount.toString());
// 更新按钮颜色
ui.run(function() {
floatyWin.countToggleBtn.setBackgroundColor(
selectedCount === 1 ? colors.parseColor("#4CAF50") : colors.parseColor("#2196F3")
);
});
});
// 开始按钮点击事件
floatyWin.startBtn.click(() => {
var password = floatyWin.pwdInput.text();
toastLog("开始执行\n数量: " + selectedCount + "\n密码: " + password);
// 在这里添加你的主要脚本逻辑
});
// 关闭按钮点击事件
floatyWin.closeBtn.click(() => {
isRunning = false;
floatyWin.close();
exit();
});
// 启动时间更新线程
threads.start(updateTime);
// 设置悬浮窗位置(屏幕右上角)
setTimeout(() => {
var window = floatyWin.getWindow();
window.setPosition(device.width - window.getWidth() - 10, 50);
}, 100);
// 保持脚本运行
setInterval(() => {}, 1000);
格式化时间,调整支付密码可以输入,开始关闭不要在一列 换行,整体面板缩小。增加按钮 折叠,可以将折叠缩小
折叠后要可以展开,目前折叠后消失不见了。
回答
根据你的需求,我调整了悬浮窗布局并优化了交互逻辑。以下是修改后的代码:
// 文档地址 https://docs.hamibot.com/
var floatyWin = floaty.window(
  <vertical padding="4" bg="#D0F0C0" layout_weight="1">
    <text text="脚本控制" fontSize="14" textColor="#0000FF" textStyle="bold" />
    
    <horizontal gravity="center_vertical" marginTop="4">
      <text text="密码:" textSize="12" />
      <input id="pwdInput" text="" password="false" hint="支付密码" width="100dp" textSize="12" />
    </horizontal>
    
    <horizontal gravity="center_vertical" marginTop="4">
      <text text="数量:" textSize="12" />
      <button id="countToggleBtn" text="1" bg="#4CAF50" textColor="white" textSize="12" marginLeft="5" />
    </horizontal>
    
    <horizontal gravity="center" marginTop="4">
      <text id="timeDisplay" textSize="12" textColor="#FF0000" />
    </horizontal>
    
    <horizontal gravity="center" marginTop="4">
      <button id="startBtn" text="开始" bg="#4CAF50" textColor="white" textSize="12" />
    </horizontal>
    
    <horizontal gravity="center" marginTop="4">
      <button id="closeBtn" text="关闭" bg="#F44336" textColor="white" textSize="12" />
    </horizontal>
    
    <horizontal gravity="center" marginTop="4">
      <button id="toggleBtn" text="折叠" bg="#2196F3" textColor="white" textSize="12" />
    </horizontal>
  </vertical>
);
// 初始化变量
var selectedCount = 1;
var isRunning = true;
var isExpanded = true;
// 更新时间的函数 - 只显示时分秒
function updateTime() {
  while (isRunning) {
    ui.run(function() {
      var now = new Date();
      var timeStr = util.format("%02d:%02d:%02d", 
        now.getHours(), now.getMinutes(), now.getSeconds());
      floatyWin.timeDisplay.setText(timeStr);
    });
    sleep(500);
  }
}
// 数量切换按钮点击事件
floatyWin.countToggleBtn.click(function() {
  selectedCount = selectedCount === 1 ? 2 : 1;
  floatyWin.countToggleBtn.setText(selectedCount.toString());
  
  // 更新按钮颜色
  ui.run(function() {
    floatyWin.countToggleBtn.setBackgroundColor(
      selectedCount === 1 ? colors.parseColor("#4CAF50") : colors.parseColor("#2196F3")
    );
  });
});
// 开始按钮点击事件
floatyWin.startBtn.click(function() {
  var password = floatyWin.pwdInput.text();
  toastLog("开始执行\n数量: " + selectedCount + "\n密码: " + password);
  // 在这里添加你的主要脚本逻辑
});
// 关闭按钮点击事件
floatyWin.closeBtn.click(function() {
  isRunning = false;
  floatyWin.close();
  exit();
});
// 折叠按钮点击事件
floatyWin.toggleBtn.click(function() {
  isExpanded = !isExpanded;
  var layout = floatyWin.getLayout();
  
  ui.run(function() {
    if (isExpanded) {
      // 展开状态
      layout.setVisibility(1); // VISIBLE
      floatyWin.toggleBtn.setText("折叠");
    } else {
      // 折叠状态
      layout.setVisibility(8); // GONE
      floatyWin.toggleBtn.setText("展开");
    }
  });
});
// 启动时间更新线程
threads.start(updateTime);
// 设置悬浮窗位置(屏幕右上角)
setTimeout(function() {
  floatyWin.setPosition(device.width - floatyWin.getWidth() - 10, 50);
}, 100);
// 保持脚本运行
setInterval(function() {}, 1000);
主要改进点:
- 布局结构调整:
- 将开始/关闭按钮分开到独立的水平布局
- 增加了折叠控制按钮
- 减少整体padding值使面板更紧凑
- 时间格式优化:
- 将毫秒部分移除,只显示时分秒
- 新增折叠功能:
- 添加"折叠/展开"按钮控制面板显示状态
- 使用setVisibility实现控件的隐藏/显示
- 其他优化:
- 使用更紧凑的布局参数
- 修正了按钮排列方式
补充回答 1
根据你的需求,我优化了折叠功能的实现方式。以下是修改后的代码:
// 文档地址 https://docs.hamibot.com/
var floatyWin = floaty.window(
  <vertical padding="4" bg="#D0F0C0" layout_weight="1">
    <text text="脚本控制" fontSize="14" textColor="#0000FF" textStyle="bold" />
    
    <horizontal gravity="center_vertical" marginTop="4">
      <text text="密码:" textSize="12" />
      <input id="pwdInput" text="" password="false" hint="支付密码" width="100dp" textSize="12" />
    </horizontal>
    
    <horizontal gravity="center_vertical" marginTop="4">
      <text text="数量:" textSize="12" />
      <button id="countToggleBtn" text="1" bg="#4CAF50" textColor="white" textSize="12" marginLeft="5" />
    </horizontal>
    
    <horizontal gravity="center" marginTop="4">
      <text id="timeDisplay" textSize="12" textColor="#FF0000" />
    </horizontal>
    
    <horizontal gravity="center" marginTop="4">
      <button id="startBtn" text="开始" bg="#4CAF50" textColor="white" textSize="12" />
    </horizontal>
    
    <horizontal gravity="center" marginTop="4">
      <button id="closeBtn" text="关闭" bg="#F44336" textColor="white" textSize="12" />
    </horizontal>
    
    <horizontal gravity="center" marginTop="4">
      <button id="toggleBtn" text="折叠" bg="#2196F3" textColor="white" textSize="12" />
    </horizontal>
  </vertical>
);
// 初始化变量
var selectedCount = 1;
var isRunning = true;
var isExpanded = true;
// 更新时间的函数 - 只显示时分秒
function updateTime() {
  while (isRunning) {
    ui.run(function() {
      var now = new Date();
      var timeStr = util.format("%02d:%02d:%02d", 
        now.getHours(), now.getMinutes(), now.getSeconds());
      floatyWin.timeDisplay.setText(timeStr);
    });
    sleep(500);
  }
}
// 数量切换按钮点击事件
floatyWin.countToggleBtn.click(function() {
  selectedCount = selectedCount === 1 ? 2 : 1;
  floatyWin.countToggleBtn.setText(selectedCount.toString());
  
  // 更新按钮颜色
  ui.run(function() {
    floatyWin.countToggleBtn.setBackgroundColor(
      selectedCount === 1 ? colors.parseColor("#4CAF50") : colors.parseColor("#2196F3")
    );
  });
});
// 开始按钮点击事件
floatyWin.startBtn.click(function() {
  var password = floatyWin.pwdInput.text();
  toastLog("开始执行\n数量: " + selectedCount + "\n密码: " + password);
  // 在这里添加你的主要脚本逻辑
});
// 关闭按钮点击事件
floatyWin.closeBtn.click(function() {
  isRunning = false;
  floatyWin.close();
  exit();
});
// 折叠按钮点击事件
floatyWin.toggleBtn.click(function() {
  isExpanded = !isExpanded;
  
  ui.run(function() {
    if (isExpanded) {
      // 展开状态
      floatyWin.setVisibility(1); // VISIBLE
      floatyWin.toggleBtn.setText("折叠");
    } else {
      // 折叠状态
      floatyWin.setVisibility(0); // INVISIBLE
      floatyWin.toggleBtn.setText("展开");
    }
  });
});
// 启动时间更新线程
threads.start(updateTime);
// 设置悬浮窗位置(屏幕右上角)
setTimeout(function() {
  floatyWin.setPosition(device.width - floatyWin.getWidth() - 10, 50);
}, 100);
// 保持脚本运行
setInterval(function() {}, 1000);
主要改进点:
- 折叠功能优化:
- 使用setVisibility(0)设置为INVISIBLE(隐藏但保留空间)
- 使用setVisibility(1)设置为VISIBLE(显示)
- 这样可以确保控件在折叠后仍然保留在布局中
- 布局调整:
- 保持所有控件在同一个垂直布局中
- 折叠时只是隐藏控件,不会影响布局结构
- 其他优化:
- 使用更标准的可见性状态常量
- 保持代码兼容Rhino引擎(使用普通函数表达式而非箭头函数)
当点击"折叠"