【问题标题】:JavaFXPorts: Is this correct behavior for javafx.scene.control.ComboBox on Android device?JavaFXPorts:这是 Android 设备上 javafx.scene.control.ComboBox 的正确行为吗?
【发布时间】:2018-03-08 21:36:41
【问题描述】:

在 ComboBox 中选择项目后,ComboBox 中不会显示此选定项目 - 只有 Android 设备,在桌面上可以。 比较这两张截图:


[选择“选项 2”时在桌面上]

[选择“选项 2”时在 Android 设备上]

我正在使用 JavaFXPorts 8.60.8。

【问题讨论】:

  • 我记得它对我也一样。旁注:JavaFX 组合框在 winXp 上也表现不佳
  • 为 JavaFXPorts 的问题跟踪器添加了错误:JavaFXPorts issue

标签: android javafx javafxports


【解决方案1】:

基于此question,并在您的bug report 中提及您使用的是三星设备,在某些三星设备中存在一个已知问题,即在 JavaFXPorts 中完成的触摸事件处理无法像在其余的 Android 设备。

虽然这是对 JavaFXPorts 的修复,但您可以尝试以下解决方法:

comboBox.setCellFactory(p -> new ListCell<String>() {

        private String item;
        {
            setOnMousePressed(e -> comboBox.getSelectionModel().select(item));
        }

        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty); 
            this.item = item;
            setText(item);
        }

    });

注意我使用了鼠标按下事件处理程序而不是鼠标单击事件处理程序。由于我无法重现它,在我的情况下,鼠标单击被列表选择事件消耗(因为这可以正常工作),但在你的情况下,您可能可以使用按下或单击事件。

【讨论】:

  • 您的解决方法有效。再次非常感谢何塞。
【解决方案2】:

根据 Josés 的回答,我实现了以下通用 Helper 函数,这可能会对你们中的一些人有所帮助:

public static <T> void removeSelectionBug(ComboBox<T> comboBox) {
    comboBox.setCellFactory(p -> new ListCell<T>() {
        private T item;
        {
            setOnMousePressed(e -> comboBox.getSelectionModel().select(item));
        }


        @Override
        protected void updateItem(T item, boolean empty) {
            super.updateItem(item, empty);
            this.item = item;
            if (item != null) {
                setText(item.toString());
            }
        }
    });
}

顺便说一句:我的所有手机(Samsung Note 3、Sony XPERIA Z3 Compact 和 Nexus 4)上都有这个错误

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 2012-12-05
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 2016-03-25
    相关资源
    最近更新 更多