【发布时间】:2021-05-22 14:07:17
【问题描述】:
这种情况就像用户触摸一个视图并保持该视图指定的秒数。 类似于长焦点侦听器但具有指定计时器的东西,如果用户在计时器之前将手指移开,那么它将不会调用操作。是否可以?请指导。
【问题讨论】:
这种情况就像用户触摸一个视图并保持该视图指定的秒数。 类似于长焦点侦听器但具有指定计时器的东西,如果用户在计时器之前将手指移开,那么它将不会调用操作。是否可以?请指导。
【问题讨论】:
public class MainActivity extends Activity {
// This example shows an Activity, but you would use the same approach if
// you were subclassing a View.
//Declare timer
CountDownTimer cTimer = null;
@Override
public boolean onTouchEvent(MotionEvent event){
int action = MotionEventCompat.getActionMasked(event);
switch(action) {
case (MotionEvent.ACTION_DOWN) :
startTimer();
Log.d(DEBUG_TAG,"Action was DOWN");
return true;
case (MotionEvent.ACTION_UP) :
cancelTimer();
Log.d(DEBUG_TAG,"Action was UP");
return true;
default :
return super.onTouchEvent(event);
}
void startTimer() {
cTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
//you can keep updating the ui here.
}
public void onFinish() {
//this is where you want to do something on the basis on long tap action.
}
};
cTimer.start();
}
void cancelTimer() {
if(cTimer!=null)
cTimer.cancel();
}
}
您可以在 docs 上查看 MotionEvents。
【讨论】: