怎么做一个弹窗的界面,给一个样例代码

已存档
HAMI_5946101677 · 1 个月前

回答

Hamibot AI
1
· 1 个月前

文档地址 https://docs.hamibot.com/

Hamibot提供了多种弹窗界面实现方式,以下是几种常用的弹窗示例代码:

1. 简单提示弹窗

// 显示一个简单的消息提示框
alert('操作成功!', '数据已保存到本地');

2. 确认对话框

var result = confirm('确定要删除该文件吗?', '删除后无法恢复');
if (result) {
    toast('文件已删除');
} else {
    toast('操作已取消');
}

3. 列表选择弹窗

var dialog = dialogs.build({
    title: "请选择操作",
    positive: "确定",
    negative: "取消",
    items: ["复制", "移动", "重命名", "删除"],
    itemsSelectMode: "singleChoice"
}).on("item_select", function(index, item) {
    toast("选择了: " + item);
}).show();

4. 输入对话框

var name = dialogs.rawInput('请输入用户名', '默认用户名', function(name) {
    if (name) {
        toast('你好, ' + name);
    }
});

5. 自定义悬浮窗(类似弹窗)

var window = floaty.window(
    <frame gravity="center" bg="#80000000">
        <vertical padding="20" bg="#FFFFFFFF" radius="10">
            <text id="title" text="提示" textSize="18sp" textColor="#000000" gravity="center"/>
            <text id="content" text="这是一个自定义弹窗" marginTop="10" gravity="center"/>
            <horizontal marginTop="20">
                <button id="cancel" text="取消" width="80" bg="#DDDDDD"/>
                <button id="ok" text="确定" width="80" bg="#2196F3" textColor="#FFFFFF" marginLeft="10"/>
            </horizontal>
        </vertical>
    </frame>
);

// 设置按钮点击事件
window.ok.on("click", function() {
    toast("你点击了确定");
    window.close();
});

window.cancel.on("click", function() {
    window.close();
});

// 5秒后自动关闭
setTimeout(function() {
    window.close();
}, 5000);

这些示例涵盖了Hamibot中常用的弹窗实现方式。简单提示使用alert/confirm,需要用户输入使用rawInput,需要复杂交互可以使用floaty.window创建自定义悬浮窗。

注意:在UI模式下(脚本开头有'ui';),confirm和input等函数会返回Promise对象,需要使用.then处理结果,例如:

'ui';
confirm('确定要继续吗?').then(function(result) {
    if (result) {
        toast('你选择了确定');
    }
});
ℹ️ 本回答由 AI 生成,内容仅供参考