如何在 android 中创建倒计时 onClickListener
how to create countdown onClickListener in android
场景就像用户触摸一个视图并保持该视图指定的秒数。
类似于长焦点侦听器,但具有指定的计时器,如果用户在计时器之前将手指移开,则它将不会调用操作。可能吗?请指导。
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
场景就像用户触摸一个视图并保持该视图指定的秒数。 类似于长焦点侦听器,但具有指定的计时器,如果用户在计时器之前将手指移开,则它将不会调用操作。可能吗?请指导。
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