本页面由葡萄牙语机器翻译,可能存在错误。葡萄牙语版本为准。
如何打开操作菜单(ActionSheet)
目的。 在屏幕底部的圆角卡片中显示一组上下文相关的操作,下方另有一个独立的「取消」 按钮(iOS 模式)。当用户需要在与某个条目相关的多个操作之间进行选择时使用(「分享」、 「删除」……)。
所用 API
new ActionSheet(String cancelText, String... buttonTexts)— 创建操作面板。第一个参数是「取消」的文本;其余为选项按钮(索引 0、1、2……)。sheet.buttonAction(int index, Component.Action action)— 该索引选项按钮的回调。sheet.cancelAction(Component.Action action)— 「取消」按钮的回调(可选;默认仅关闭)。sheet.buttonStyle(int index, ComponentStyle style)— 单个按钮的样式(例如:把「删除」染成红色)。sheet.buttonStyle(ComponentStyle style)— 所有选项按钮的默认样式。sheet.cancelStyle(ComponentStyle style)/sheet.containerStyle(ComponentStyle style)— 「取消」按钮和容器卡片的样式。sheet.maxColumns(int cols)— 按钮很多时的最大列数(默认 1;仅在大于 1 时生效)。ActionSheet.createDefaultButtonStyle()— 现成的基础样式(白色背景、蓝色文字、pressable),供你复制和修改。sheet.show()— 显示(继承自Window)。
不易察觉的行为:与
Alert一样,没有buttonAction的按钮被点击时仅关闭 面板。「取消」默认也只是关闭。
完整示例(复制、粘贴即可运行)
package com.exemplo;
import dev.crossfly.ui.*;
import dev.crossfly.ui.layout.*;
/** 可执行的主界面。将 launcher 指向此类(classname)并运行:gradlew :lwjgl3:run */
public class MainWindow extends Window
{
@Override
public void onShow() throws Exception
{
super.onShow();
setLayout(new VerticalLayout());
// 触发按钮:点击时打开 ActionSheet
Button btn = new Button("Opções");
btn.onAction(origin ->
{
ActionSheet sheet = new ActionSheet(
"Cancelar", // 取消按钮的文本(第 1 个参数)
"Compartilhar", // 索引 0
"Excluir"); // 索引 1
sheet.buttonAction(0, o ->
{
compartilhar();
sheet.hide();
});
// 将破坏性操作以红色突出显示
sheet.buttonStyle(1, ActionSheet.createDefaultButtonStyle().textColor(0xFFFF0000));
sheet.buttonAction(1, o ->
{
excluir();
sheet.hide();
});
sheet.show();
});
add(LAY_FILL, btn);
}
private void compartilhar()
{
// ... 你的分享逻辑 ...
}
private void excluir()
{
// ... 你的删除逻辑 ...
}
}
下面的片段只展示核心部分。要运行它,请把它放进上面的
MainWindow骨架中。
最小片段
ActionSheet sheet = new ActionSheet(
"Cancelar", // 取消按钮的文本(第 1 个参数)
"Compartilhar", // 索引 0
"Excluir"); // 索引 1
sheet.buttonAction(0, origin ->
{
compartilhar();
sheet.hide();
});
// 将破坏性操作以红色突出显示
sheet.buttonStyle(1, ActionSheet.createDefaultButtonStyle().textColor(0xFFFF0000));
sheet.buttonAction(1, origin ->
{
excluir();
sheet.hide();
});
sheet.show();
验证说明(2026 代码)。 已在源码中确认:
ActionSheet(cancelText, String... buttonTexts)、buttonAction(int, Action)、cancelAction(Action),并存在buttonStyle(int, ComponentStyle)、buttonStyle(ComponentStyle)、maxColumns(int)。默认外观(iOS 卡片) 无论 Skin 为何都会应用,但defaultStyleCreate()会为窗口使用getSkin().actionSheet()。 默认宽度约为屏幕的 92%(widthRatio ≈ .92f)。
深入了解
- 控件页面:ActionSheet · 权威来源:第 16 章
- 姊妹配方:用 Alert 进行确认 · 显示 Hint
这个页面有帮助吗?感谢你的反馈!