Android 单击按钮后如何检查持续时间
我不熟悉Android 单击按钮后如何检查持续时间,android,handler,runnable,ontouchlistener,android-timer,Android,Handler,Runnable,Ontouchlistener,Android Timer,我不熟悉Java和Android,我想在ACTION\u up事件中设置一个时间计数器,并在执行其他事件时取消计时器。我怎样才能基本上为该事件设置计时器,并停止和重置其他事件的计时器?您必须尝试此方法 public boolean onTouchEvent(MotionEvent event) { boolean touch; switch(event.getAction()){ case MotionEvent.ACTION_DOWN:
Java
和Android
,我想在ACTION\u up
事件中设置一个时间计数器,并在执行其他事件时取消计时器。我怎样才能基本上为该事件设置计时器,并停止和重置其他事件的计时器?您必须尝试此方法
public boolean onTouchEvent(MotionEvent event) {
boolean touch;
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
touch = false;
break;
case MotionEvent.ACTION_UP:
touch = true;
// Code for Timer
break;
}
return true;
}
您必须在该代码中编写处理程序,并在其他事件中重置处理程序。
处理程序的代码
new Handler().postDelayed(new Runnable() {
public void run() {
//Your Task
}
}, TIME);
对于
倒计时
这里,我启动了30秒的时间计时器,如
CountDownTimer countDownTimer;
TextView tvTicker = (TextView) findViewById(R.id.tvTicker);
public void startClicked(View view) { //When button start is clicked
countDownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
tvTicker.setText("seconds remaining: " + millisUntilFinished / 1000);
//Do some stuff here for saving the duration to a variable or anything else as your requirements
}
public void onFinish() {
tvTicker.setText("done!");
}
}.start();
}
方法说明
CountDownTimer(long millisInFuture, long countDownInterval)
millisInFuture
=以毫秒为单位的时间
倒计时间隔
=以毫秒为单位的间隔
现在,您可以将这些方法用于其他类型的操作
countDownTimer.cancel(); //Cancel the countdown.
countDownTimer.onFinish() //Callback fired when the time is up.
countDownTimer.onTick(long millisUntilFinished); //Callback fired on regular `interval. millisUntilFinished is The amount of time until finished.`
计时器开始时间。 在启动计时器单击事件中设置此选项
Date startDate = new Date();
long startTime = 0;
startTime = startDate.getTime();
Date endDate = new Date();
long endTime = 0;
endTime = endDate.getTime();
将startTime存储在全局变量中,以便以后可以使用该变量
计时器结束时间。
在停止计时器单击事件中设置此选项
Date startDate = new Date();
long startTime = 0;
startTime = startDate.getTime();
Date endDate = new Date();
long endTime = 0;
endTime = endDate.getTime();
现在在毫秒内获得时差
long timeDiff = endTime - startTime;
您想得到刚开始和结束的时差吗?您的解决方案有效,非常感谢!你刚刚救了我一天!事实上你的方法对我有用!谢谢你的帮助!