跳到内容
CrossFly 是 100% 开源在 GitHub 关注我们
CrossFlyCrossFly
开始使用
全部示例
配方列表输入81 行 · 1 个文件

Choosing from a list

ListViewWindow does single and multi selection, with the title, the confirm and the tap-to-choose already wired.

它展示了什么

Picking one from a list, and picking several — without building either by hand.

This recipe exists because the reference app on this framework wrote its own single-choice sheet while ListViewWindow sat unused. Reading this one screen is cheaper than rediscovering it.

在你的机器上运行

每个示例都是普通的 Window,启动器只需要它的名字。

gradlew lwjgl3:run --args="classname dev.cf.recipe.R06Select"
修改它

Java 是提前编译的,浏览器无法自行重建你的修改 —— 这里的代码在发布前已由 TeaVM 转成 JavaScript。把文件复制到项目里,它在四个平台上原样运行;上面那条命令就是全部准备工作。

正在此处运行CrossFly 0.1.32
正在启动应用…

由 TeaVM 编译为 JavaScript —— 是同一份代码,不是录像。 · 全部示例

它用到了什么

本示例触及的框架类型。带链接的有参考页。

ButtonComponentDebugLabelListViewWindowScreenStylesWidgets

源码

完整无删减。这就是那个文件,不是片段。

原始
dev/cf/recipe/R06Select.java
package dev.cf.recipe;

import dev.cf.Tones;

import dev.crossfly.ui.*;
import dev.crossfly.ui.color.*;
import dev.crossfly.ui.graphics.*;
import dev.crossfly.ui.layout.*;
import dev.crossfly.ui.style.*;

import static dev.crossfly.ui.Widgets.*;

/**
 * Picking one from a list, and picking several — without building either by hand.
 *
 * <p>This recipe exists because the reference app on this framework wrote its own single-choice
 * sheet while {@link ListViewWindow} sat unused. Reading this one screen is cheaper than
 * rediscovering it.</p>
 */
public class R06Select extends Recipe
{
   private static final String[] OPCOES = { "Beverages", "Food", "Cleaning", "Hygiene", "Bakery",
      "Frozen", "Pet" };

   private Label escolha;

   public String title()
   {
      return "Choosing from a list";
   }

   public String note()
   {
      return "ListViewWindow does single and multi selection, with the title, the confirm and the"
         + " tap-to-choose already wired.";
   }

   public void build(Component body) throws Exception
   {
      escolha = label("nothing chosen yet", 1f, false, Styles.captionColor, Position.START);
      escolha.testTag("recipe.choice");

      Button um = Tones.action("Pick one");
      um.testTag("recipe.pick.one");
      um.onAction(c -> abrir(ListViewWindow.ItemType.SINGLE));

      Button varios = new Button("Pick several");
      varios.testTag("recipe.pick.many");
      varios.onAction(c -> abrir(ListViewWindow.ItemType.MULTIPLE));

      body.add(LAY_AUTO, outgap(um, 0, 0, 0, .6f));
      body.add(LAY_AUTO, outgap(varios, 0, 0, 0, 1f));
      body.add(LAY_AUTO, escolha);
   }

   private void abrir(ListViewWindow.ItemType tipo)
   {
      try
      {
         final ListViewWindow janela = new ListViewWindow("Segment", OPCOES, tipo);
         // SINGLE closes on the tap; MULTIPLE waits for the confirm. Both arrive here.
         janela.onConfirm(c ->
         {
            int[] idx = janela.getSelectedIndices();
            StringBuilder sb = new StringBuilder();
            for (int i : idx)
               sb.append(sb.length() == 0 ? "" : ", ")
                  .append(OPCOES[i]);
            escolha.setText(sb.length() == 0 ? "nothing chosen" : sb.toString());
            escolha.setValidLayout(false);
            Screen.repaint();
         });
         janela.show();
      }
      catch (Exception e)
      {
         dev.crossfly.util.Debug.handle(e);
      }
   }
}
给 AI 助手

本页的机器可读版本,让模型直接读取示例集,而不是猜测 API。

本示例(JSON)本示例(Markdown)
上一个Listing: cards or table下一个Picking a date range