我有一个按钮,按下并继续按住,如果按住时间超过了某个时间间隔,它会触发某种意图。我该如何实现这个功能?谢谢。
试试这个。
你可以使用Touch Listener
来实现这个。
尝试:
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;
}
});
在您的情况下,b
应该是您想要进行长按的view
(通常是一个按钮)。
而Runnable
run
的代码如下:
Runnable run = new Runnable() {
@Override
public void run() {
// Your code to run on long click
}
};
希望能对您有所帮助... :)
检测 [ACTION_DOWN][1]
和 [ACTION_UP][2]
事件。
当按钮被按下 ([ACTION_DOWN][1]
),开始计时,计算时间... 如果计时器超时,则调用 Intent。当按钮被释放 ([ACTION_UP][2]
),停止计时器。
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;
}
});
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;
}
}
};
当我使用计时器时,出现了异常!您也可以像下面这样使用处理程序 -
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;
}});
ACTION_DOWN
和 ACTION_UP
之间的偏移量;ACTION_UP
时将其删除。还包括如何获取精确的时间值...@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);
}
编辑。
显然,我误解了问题。请参见上面的@Hardik和@Lunar的解决方案。
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.
}
});