简体   繁体   中英

OnTouchListener() will only execute once while button is pressed and held down

brown = (Button) findViewById(R.id.brownButton);
    brown.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                count++;
                Log.d("count", "" + count);
                return true;
            } else if (event.getAction() == (MotionEvent.ACTION_UP)) {
                count--;
                Log.d("count", "" + count);
                return true;

            }
            return false;

        }
    });

When my finger presses and holds the button my count will only increment ONCE. When I let go it will decrement accordingly. Please can someone show me how I can get my code to increment as long as my finger is holding the button down. Thanks.

I also needed to do this, but this question was never adequately answered, even though it is fairly old. However, a good solution can be found here: android repeat action on pressing and holding a button

I am adapting that solution to this problem. I tested it and it works for me.

Button brown = (Button) findViewById(R.id.brownButton);
brown.setOnTouchListener(new View.OnTouchListener() {

    private Handler mHandler;
    private int count = 0;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (mHandler != null)
                return true;
            mHandler = new Handler();
            mHandler.postDelayed(mAction, 500);
            break;
        case MotionEvent.ACTION_UP:
            if (mHandler == null)
                return true;
            mHandler.removeCallbacks(mAction);
            mHandler = null;
            break;
        }
        return true; // before I had written false
    }

    Runnable mAction = new Runnable() {
        @Override
        public void run() {
            Log.d("count: ", "" + count);
            count++;
            mHandler.postDelayed(this, 500);
        }
    };

});

In the future I will apply this solution to a custom soft keyboard to allow the space and delete keys to continue working when being held down.

Update: Today I finally got around to making the continuous delete key. I thought I had tested my above code before, but today when I was doing it I had to return true from the onTouch method in order to get it to work.

A touch listener works like this:

It receives a ACTION_DOWN event (the user touches)

It then receives all the following events (if true is returned) and treats all the following events as ACTION_MOVE events

The touch lisetener keeps receiving ACTION_MOVE events until it receives an ACTION_UP or ACTION_CANCEL event.

So ACTION_DOWN is only triggered once, then any number of ACTION_MOVE events, then either a single ACTION_UP or ACTION_CANCEL.

If you want your counter to increment you need to check for ACTION_MOVE events.

@Override
public boolean onTouch(View v, MotionEvent event) {

    switch(event.getAction()){
    case MotionEvent.ACTION_DOWN:
     //do something on DOWN 
         break;
    case MotionEvent.ACTION_MOVE:
         //do something on MOVE
         break;
    case MotionEvent.ACTION_UP:
     //do something on UP           
         break;
    }
    return true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM