How can I get button pressed time when I holding b

2019-02-06 21:19发布

I have a button, I press it and continue to holding it, if holding time exceeds certain time interval it fires some kind of intent, how can i do this. Thanks

6条回答
祖国的老花朵
2楼-- · 2019-02-06 21:51

I got an Exception when I used a Timer! You can also use a Handler like below -

final Handler handler = new Handler();
final Runnable run = new Runnable(){
        @Override
        public void run() {
            //Do your Stuff!     
        }
    };
    button.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(final View view, MotionEvent motion) {
                switch (motion.getAction() ) {
                    case MotionEvent.ACTION_DOWN:
                        handler.postDelayed(run, 5000); //5 seconds delay
                        return true;

                    case MotionEvent.ACTION_UP:
                        handler.removeCallbacks(run);
                        return true;
                        }
                        return false;
                    }});
查看更多
Melony?
3楼-- · 2019-02-06 21:55

Detect [ACTION_DOWN][1] and [ACTION_UP][2] events.

When button is pressed ([ACTION_DOWN][1]), start timer, measure time... If timer exceeds, invoke Intent. When button is released ([ACTION_UP][2]) stop timer.

final Timer timer = new Timer();
button.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {
        switch ( arg1.getAction() ) {
        case MotionEvent.ACTION_DOWN:
            //start timer
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    // invoke intent
                }
            }, 5000); //time out 5s
            return true;
        case MotionEvent.ACTION_UP:
            //stop timer
            timer.cancel();
            return true;
        }
        return false;
    }
});

When button will be pressed, timer will be started. If button will be released before 5 seconds timeout, nothing will happen, otherwise your desired action will be executed.

查看更多
做个烂人
4楼-- · 2019-02-06 21:56

try this

You can use Touch Listener to do this.

Try:

Handler handler = new Handler();
    b.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            switch (arg1.getAction()) {
            case MotionEvent.ACTION_DOWN:
                handler.postDelayed(run, 5000/* OR the amount of time you want */);
                break;

            default:
                handler.removeCallbacks(run);
                break;

            }
            return true;
        }
    });

Where b is the view (in your case it should button) on which you want to make long click.

And Runnable run is as follows

Runnable run = new Runnable() {

    @Override
    public void run() {
        // Your code to run on long click

    }
};

Hope it will help... :)

查看更多
我命由我不由天
5楼-- · 2019-02-06 21:58

Edit.

Apparently, I have misunderstood the question. Please see @Hardik and @Lunar solutions above.


You could get away with doing something like this:

button.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        long duration = 0L;

        final int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                duration = SystemClock.elapsedRealtime();
                break;
            case MotionEvent.ACTION_UP:
                duration = SystemClock.elapsedRealtime() - duration;
                break;
            default:
                break;
        }

        if (duration > YOUR_INTERVAL_IN_MILLIS) {
            // Do stuff...
        }

        return true; // or whatever you want to return.
    }
});
查看更多
Animai°情兽
6楼-- · 2019-02-06 22:07

You can override onTouchEvent and calculate the offset between ACTION_DOWN and ACTION_UP;

Code below will run a runnable after x amount of time and also remove it if ACTION_UP is called before x time. Also included how you can get an exact time value...

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        // record the start time
        mStartTime = ev.getEventTime();
        mHandler.postDelayed(mTaskToRun, MAXIMUM_LONG_PRESS_INTERVAL);
    } else if (ev.getAction() == MotionEvent.ACTION_UP) {
        // record the end time
        mEndTime = ev.getEventTime();

        long keyPressedDuration = mEndTime - mStartTime;

        mHandler.removeCallbacks(mTaskToRun);
    }

    return super.onTouchEvent(ev);
}
查看更多
霸刀☆藐视天下
7楼-- · 2019-02-06 22:09
long lastDown;
long keyPressedDuration ;

button.setOnTouchListener(new OnTouchListener() {
     @Override
     public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
           lastDown = System.currentTimeMillis();
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
           keyPressedDuration = System.currentTimeMillis() - lastDown;
        }
     }
  };
查看更多
登录 后发表回答