长时间保留的文本字段复制操作(复制弹出窗口) [英] Textfield copy action on long hold (copy popup)

查看:105
本文介绍了长时间保留的文本字段复制操作(复制弹出窗口)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Textfield,我想在我的Android设备上复制它的内容。

I have Textfield and I want to copy content of it on my android device.

如果我将它作为Windows桌面应用程序运行,我可以选择文本右键单击我得到了弹出菜单,可能有什么行动。

If I run it as a windows desktop application I can select the text right click I got the popup menu with possible actions.

有没有在Android上获得这个弹出菜单

Is there anyway to get this popup menu also on Android?

推荐答案

可以在JavaFX中轻松模拟推送和保持事件,如此问题

The push and hold event can be easily simulated in JavaFX, as in this question.

获得初始事件后,您需要做的就是从TextField调用ContextMenu。由于 TextField.getContextMenu()不会返回默认值,因此您可以提供自己的或尝试获取默认值。

Once you get the initial event, all you need to do is call the ContextMenu from the TextField. Since TextField.getContextMenu() won't return the default one, you can either provide your own or try to get the default one.

获取默认值有点棘手,因为它是 TextFieldBehavior 类的一部分。它包含此方法 public void contextMenuRequested(ContextMenuEvent e); ,所以您需要做的就是提供 ContextMenuEvent ,从TextField中激活事件。

Getting the default one is a little bit more tricky, since it is part of the TextFieldBehavior class. It contains this method public void contextMenuRequested(ContextMenuEvent e);, so all you need to do is provide a ContextMenuEvent, and fire the event from the TextField.

这是一个快速实现:

public class BasicView extends View {

    public BasicView(String name) {
        super(name);

        TextField textField = new TextField();

        addPressAndHoldHandler(textField, Duration.seconds(1), event -> {
            Bounds bounds = textField.localToScreen(textField.getBoundsInLocal());
            textField.fireEvent(new ContextMenuEvent(ContextMenuEvent.CONTEXT_MENU_REQUESTED, 
                    0, 0, bounds.getMinX() + 10, bounds.getMaxY() + 10, false, null));
        });

        setCenter(new VBox(15.0, new Label("Push and hold for ContextMenu"), textField));
    }

    private void addPressAndHoldHandler(Node node, Duration holdTime, EventHandler<MouseEvent> handler) {
        class Wrapper<T> { 
            T content; 
        }

        Wrapper<MouseEvent> eventWrapper = new Wrapper<>();

        PauseTransition holdTimer = new PauseTransition(holdTime);
        holdTimer.setOnFinished(event -> handler.handle(eventWrapper.content));

        node.addEventHandler(MouseEvent.MOUSE_PRESSED, event -> {
            eventWrapper.content = event;
            holdTimer.playFromStart();
        });
        node.addEventHandler(MouseEvent.MOUSE_RELEASED, event -> holdTimer.stop());
        node.addEventHandler(MouseEvent.DRAG_DETECTED, event -> holdTimer.stop());
    }

    @Override
    protected void updateAppBar(AppBar appBar) {
        appBar.setTitleText("Push and Hold");
    }

}

在桌面上这就是你的' ll get:

On Desktop this is what you'll get:

好消息是你不需要为Android修改ContextMenu,JavaFX有一个自定义的:

The good news is you don't need to modify the ContextMenu for Android, JavaFX has a custom one:

请注意,不同的菜单项会根据上下文,如桌面弹出窗口。

Note the different menu items will change automatically based on the context, as in the Desktop popup.

这篇关于长时间保留的文本字段复制操作(复制弹出窗口)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆