Button to execute an action while pressing it for a long time

2

Dear

I need a button while pressed to execute an action X example

While I press to increase a counter and when I stop pressing, I'll stop

    
asked by Edwin S Toala P 23.04.2017 в 20:20
source

2 answers

2

You can do it with an Asyncrona Task like this:

    public class MainActivity extends AppCompatActivity {

    Button btnContador;
    boolean estaPresionado = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnContador = (Button) findViewById(R.id.btnContador);

        btnContador.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {

                    case MotionEvent.ACTION_DOWN:

                        if (!estaPresionado) {
                            estaPresionado = true;
                            new EnviarContadorTarea().execute();
                        }
                        break;
                    case MotionEvent.ACTION_UP:

                        estaPresionado = false;

                }
                return true;
            }

        });
    }

    private class EnviarContadorTarea extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... arg0) {

            while(estaPresionado) {
                AumentarContador();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            return null;
        }

        private void AumentarContador(){
            int contador = 0;
            contador = contador+1;
            Log.d("CONTADOR", "Valor: " + String.valueOf(contador));
        }

    }
}

In your activity put an inner class AsyncTask and there you put the action you want to happen while a certain value is true, in our case, while the button is pressed.

Setting a Boolean variable to say if the button is pressed or not, if the button is held down, this variable will remain True and if this variable remains true, the action of the Thread will be repeated until it is no longer true, adding also a sleep, so that the action has a small pause always before happening.

    
answered by 24.04.2017 / 00:22
source
1

By means of an Asynctask you can perform the task that causes a counter to be incremented.

  //Define variable para validar estado.
  private boolean presionado = false;

    //Obtiene referencia de boton.
    Button myButton = (Button) findViewById(R.id.btnClic);
    //Asigna OnTouchListener
    myButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if (!presionado) {
                        presionado = true;
                        //AsyncTask que ejecuta Tarea.
                        new AsyncTaskCounter().execute();
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    presionado = false;
                    Log.d("CONTADOR", "Detiene contador");
                    break;
            }
            return true;
        }

    });

By means of this Asynctask , the task taskIncrementaContador is executed:

private class AsyncTaskCounter extends AsyncTask<Void, Void, Void> {

    int contador = 0;

    @Override
    protected Void doInBackground(Void... arg0) {
        while(presionado) {
            taskIncrementaContador();
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                Log.d("ERROR", e.getMessage());
            }
        }
        return null;
    }

    private void taskIncrementaContador(){
        contador++;
        Log.d("CONTADOR", "Valor: " + String.valueOf(contador));
    }

}

Pressing prints the value of the counter every n milliseconds (in this case 500) and prints the value of the counter, when finished it writes the message that defines the button stopped being pressed.

Example:

Valor: 1
Valor: 2
Valor: 3
Valor: 4
Valor: 5
Detiene contador
Valor: 1
Valor: 2
Valor: 3
Detiene contador
    
answered by 24.04.2017 в 06:13