The action of copying a text field with a long hold (pop-up copy window)

I have a Textfield and I want to copy its contents on my Android device.

If I launched it as a Windows desktop application, I can select the text right clickI received popup menuwith possible actions.

Is there any way to get this one popup menualso on Android?

+4
source share
2 answers

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

, , , ContextMenu TextField. TextField.getContextMenu() , , .

, TextFieldBehavior. public void contextMenuRequested(ContextMenuEvent e);, , , ContextMenuEvent 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");
    }

}

, :

W9iSb.png

, ContextMenu Android, JavaFX :

Lopct.png

, , .

+5

-, " ". :

TextView tv;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv = (TextView) findViewById(R.id.tv1);
            registerForContextMenu(tv);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
    {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Select The Action");
        menu.add(0, v.getId(), 0, "Copy");//groupId, itemId, order, title
    }
    @Override
    public boolean onContextItemSelected(MenuItem item){
        if(item.getTitle()=="Copy"){
            String text = tv.getText().toString();
            Log.e("onContextItemSelected",text);
        }

        return true;
    }

textView, "onCreateContextMenu". "OnContextItemSelected", . "", . , registerForContextMenu (tv).

0

Source: https://habr.com/ru/post/1660568/


All Articles