0

setOnTouchListener我想通过使用withonTouch方法检测按钮的按下时间

我怎么能做到这一点?

4

2 回答 2

0

也许是这样的:

long mLastClickTime;

findViewById(R.id.button).setOnTouchListener(new OnTouchListener() {
      @Override
      public void onTouch(View v, MotionEvent event) {
         if (event.getAction() == MotionEvent.ACTION_DOWN) 
            mLastClickTime = SystemClock.elapsedRealtime();

         else if (event.getAction() == MotionEvent.ACTION_UP) {
            long duration = SystemClock.elapsedRealtime() - mLastClickTime;
            // using threshold of 1000 ms
            if (duration > 1000) {
               // do your magic here
            }
         }
      }    
});
于 2019-06-03T18:02:56.417 回答
0

你可以使用onTouchListener

long FirstTouchTime ;
long duration ; 

public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) 
        FirstTouchTime = System.currentTimeMillis();    

    else if (event.getAction() == MotionEvent.ACTION_UP) {
        duration = System.currentTimeMillis(); - FirstTouchTime ;
        //duration value in millisecond 
    }
}
于 2019-06-03T17:02:40.593 回答