Repeat by pressing the long button

Say you have TextViewone that displays a number like 0, and you have Button. Now, if the user presses the button, the number in the TextView will increase by one (I know how to do this), but if the user presses the button and does not release it, then the number in TextViewshould be increased should repeat it himself, as the user does not release Button. In other words: How to increase the number again and again while the user holds the button?

+5
source share
3 answers

A general approach (not specific to Android) will be to detect events for the press and release separately. A press event starts a periodic task ( Runnableor Thread), which is added to the counter (say, 5 times per second or once every 200 ms). The release event stops a periodic task.

+6
source

You need to schedule an asynchronous recurring event when you receive the event mousePressedand stop it when you receive the event mouseReleased.

There are many ways to handle this in Java. I like to use classes java.util.concurrentthat are pretty flexible. There are a few things to keep in mind:

Thread Dispatch Thread, JButton, SwingUtilities.invokeLater().

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Frame
{
    public static void main( String[] args )
    {
        JFrame frame = new JFrame( );
        final JButton button = new JButton( "0" );

        final ScheduledExecutorService executor = Executors.newScheduledThreadPool( 1 );

        button.addMouseListener( new MouseAdapter( )
        {
            int counter = 0;
            ScheduledFuture<?> future;

            @Override
            public void mousePressed( MouseEvent e )
            {
                Runnable runnable = new Runnable( )
                {
                    public void run( )
                    {
                        SwingUtilities.invokeLater( new Runnable( )
                        {
                            public void run( )
                            {
                                button.setText( String.valueOf( counter++ ) );
                            }
                        } );
                    }
                };

                future = executor.scheduleAtFixedRate( runnable, 0, 200, TimeUnit.MILLISECONDS );
            }

            @Override
            public void mouseReleased( MouseEvent e )
            {
                if ( future != null )
                {
                    future.cancel( true );
                }
            }

        } );

        frame.add( button );
        frame.setSize( 400, 400 );
        frame.setVisible( true );
    }
}
+2
  • Customize View.OnLongClickListenerfor your button
  • Give your activity Runnableand initialize (but don't run it) when loading activity
  • Ask OnLongClickListener to perform the usual asynchronous task of updating the text field and checking the time since it was first clicked.
  • Create OnTouchListenerone that pauses Runnablewhen viewing the touch event again.

I know this is a draft, but it is a really useful template to be able to reuse and modify, so it’s worth lowering your claws into it ...

+1
source

All Articles