Esta página fue traducida automáticamente del portugués y puede contener errores. La versión en portugués es la canónica.
Cómo abrir un menú de acciones (ActionSheet)
Propósito. Mostrar una lista de acciones contextuales en una tarjeta redondeada en la base de la pantalla, con un botón “Cancelar” separado debajo (patrón iOS). Úsalo cuando el usuario necesita elegir entre varias acciones relacionadas con un elemento (“Compartir”, “Eliminar”…).
APIs usadas
new ActionSheet(String cancelText, String... buttonTexts)— crea la hoja. El primer argumento es el texto del Cancelar; los demás son los botones de opción (índice 0, 1, 2…).sheet.buttonAction(int index, Component.Action action)— callback del botón de opción de ese índice.sheet.cancelAction(Component.Action action)— callback del botón Cancelar (opcional; por defecto solo cierra).sheet.buttonStyle(int index, ComponentStyle style)— estilo por botón (ej.: pintar “Eliminar” de rojo).sheet.buttonStyle(ComponentStyle style)— estilo por defecto para todos los botones de opción.sheet.cancelStyle(ComponentStyle style)/sheet.containerStyle(ComponentStyle style)— estilos del Cancelar y de la tarjeta contenedora.sheet.maxColumns(int cols)— número máximo de columnas cuando hay muchos botones (default 1; solo surte efecto por encima de 1).ActionSheet.createDefaultButtonStyle()— estilo base listo (fondo blanco, texto azul,pressable) para que lo copies y modifiques.sheet.show()— muestra (heredado deWindow).
Comportamiento no obvio: como en el
Alert, los botones sinbuttonActionsolo cierran la hoja al ser tocados. El Cancelar también cierra por defecto.
Ejemplo completo (copiar, pegar y ejecutar)
package com.exemplo;
import dev.crossfly.ui.*;
import dev.crossfly.ui.layout.*;
/** Pantalla principal ejecutable. Apunta el launcher (classname) y corre: gradlew :lwjgl3:run */
public class MainWindow extends Window
{
@Override
public void onShow() throws Exception
{
super.onShow();
setLayout(new VerticalLayout());
// Botón disparador: abre el ActionSheet al ser tocado
Button btn = new Button("Opções");
btn.onAction(origin ->
{
ActionSheet sheet = new ActionSheet(
"Cancelar", // texto del botón Cancelar (1er argumento)
"Compartilhar", // índice 0
"Excluir"); // índice 1
sheet.buttonAction(0, o ->
{
compartilhar();
sheet.hide();
});
// Resaltar la acción destructiva en rojo
sheet.buttonStyle(1, ActionSheet.createDefaultButtonStyle().textColor(0xFFFF0000));
sheet.buttonAction(1, o ->
{
excluir();
sheet.hide();
});
sheet.show();
});
add(LAY_FILL, btn);
}
private void compartilhar()
{
// ... tu lógica de compartir ...
}
private void excluir()
{
// ... tu lógica de eliminación ...
}
}
El fragmento de abajo muestra solo el núcleo. Para ejecutarlo, colócalo en el esqueleto
MainWindowde arriba.
Fragmento mínimo
ActionSheet sheet = new ActionSheet(
"Cancelar", // texto del botón Cancelar (1er argumento)
"Compartilhar", // índice 0
"Excluir"); // índice 1
sheet.buttonAction(0, origin ->
{
compartilhar();
sheet.hide();
});
// Resaltar la acción destructiva en rojo
sheet.buttonStyle(1, ActionSheet.createDefaultButtonStyle().textColor(0xFFFF0000));
sheet.buttonAction(1, origin ->
{
excluir();
sheet.hide();
});
sheet.show();
Nota de verificación (código 2026). Confirmado en la fuente:
ActionSheet(cancelText, String... buttonTexts),buttonAction(int, Action),cancelAction(Action), y existenbuttonStyle(int, ComponentStyle),buttonStyle(ComponentStyle),maxColumns(int). El aspecto por defecto (tarjeta iOS) se aplica independientemente del Skin, perodefaultStyleCreate()usagetSkin().actionSheet()para la ventana. El ancho por defecto es ~92% de la pantalla (widthRatio ≈ .92f).
Profundiza
- Página del widget: ActionSheet · Fuente canónica: Capítulo 16
- Recetas hermanas: Confirmar con Alert · Mostrar un Hint