Java 倒计时如何实现onfinish方法
我有一个倒计时计时器,我想在finish方法或某种代码上实现它,这样当计时器停止时,文本视图将更改为Time's up,并启动另一个方法(在活动中)。 为了说明这一点,计时器有一个起始数字,以Java 倒计时如何实现onfinish方法,java,android,android-studio,countdowntimer,Java,Android,Android Studio,Countdowntimer,我有一个倒计时计时器,我想在finish方法或某种代码上实现它,这样当计时器停止时,文本视图将更改为Time's up,并启动另一个方法(在活动中)。 为了说明这一点,计时器有一个起始数字,以xx:xx的格式从零开始倒计时 计时器的类别: public class countdown_timer { private long pls; private long millisInFuture; private long countDownInterval; private boolean sta
xx:xx
的格式从零开始倒计时
计时器的类别:
public class countdown_timer {
private long pls;
private long millisInFuture;
private long countDownInterval;
private boolean status;
public countdown_timer(long pMillisInFuture, long pCountDownInterval) {
this.millisInFuture = pMillisInFuture;
this.countDownInterval = pCountDownInterval;
this.pls = pMillisInFuture;
status = false;
Initialize();
}
public void Stop() {
status = false;
}
public void Reset() {
millisInFuture = pls;
}
public long getCurrentTime() {
return millisInFuture;
}
public void Start() {
status = true;
}
public void Initialize()
{
final Handler handler = new Handler();
Log.v("status", "starting");
final Runnable counter = new Runnable(){
public void run(){
long sec = millisInFuture/1000;
if(status) {
if(millisInFuture <= 0) {
Log.v("status", "done");
} else {
Log.v("status", Long.toString(sec) + " seconds remain");
millisInFuture -= countDownInterval;
handler.postDelayed(this, countDownInterval);
}
} else {
Log.v("status", Long.toString(sec) + " seconds remain and timer has stopped!");
handler.postDelayed(this, countDownInterval);
}
}
};
handler.postDelayed(counter, countDownInterval);
}
首先,在timer类中扩展倒数计时器
public class countdown_timer extends CountDownTimer {
}
这允许您实现一些方法
@Override
public void onTick(long l) {
}
@Override
public void onFinish() {
}
您还必须实现与超级类匹配的构造函数。还可以添加一些附加参数。例如TextView
TextView textView;
public countdown_timer(long millisInFuture, long countDownInterval, TextView txt) {
super(millisInFuture, countDownInterval);
textView = txt;
}
onFinish()
就是您想要的。还要确保将该类用作倒计时
。然后你就可以启动计时器了。
希望有帮助。您可以使用:
或:
扩展倒计时
类:
public class countdown_timer extends CountDownTimer {
TextView textView;
@Override
public void onTick(long millisInFuture) {
long sec = millisInFuture/1000;
if(millisInFuture <= 0) {
Log.v("status", "done");
} else {
Log.v("status", Long.toString(sec) + " seconds remain and timer has stopped!");
}
}
@Override
public void onFinish() {
if(textView != null){
// change text in your textview
}
}
public countdown_timer(long pMillisInFuture, long pCountDownInterval) {
super(pMillisInFuture, pCountDownInterval);
}
public countdown_timer(TextView textView, long pMillisInFuture, long pCountDownInterval) {
super(pMillisInFuture, pCountDownInterval);
this.textView = textView;
}
}
更新3:如果最后一个勾号为1,则不是零,将倒计时间隔更改为500而不是1000:
public class MainActivity extends AppCompatActivity {
TextView textView;
CountDownTimer mycounterdown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
long startcard = 10000;
textView = (TextView) findViewById(R.id.test);
mycounterdown = new CountDownTimer(startcard, 500) {
@Override
public void onTick(long mycounterup) {
int minutes_up_start = (int) (mycounterup / 1000) / 60;
int seconds_up_start = (int) (mycounterup / 1000) % 60;
String time_2_up_start_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_up_start, seconds_up_start);
textView.setText(time_2_up_start_formatted);
}
@Override
public void onFinish() {
// call here other methods from activity
testMethod();
}
};
mycounterdown.start();
}
public void testMethod(){
Toast.makeText(MainActivity.this, "Test Method called", Toast.LENGTH_SHORT).show();
}
}
注意:看看这个您可以尝试在计时器中扩展
倒计时
class@Cagri雅尔辛,你能告诉我我是怎么做到的吗?为什么你不使用内置的倒计时类?@godot你是什么意思?@manos检查我的答案。希望有帮助。当我将类扩展到CountDownTimer时,它显示了以下错误:public countdown\u timer(long pmillinfuture,long pCountDownInterval)“没有可用的默认构造函数”您必须实现与您的超类匹配的构造函数。编辑我的回答到目前为止还不错,但完成时方法在timer类中,而不是在我的活动中,因此我无法使用它来更改文本。您可以更改构造函数方法来发送文本视图。检查我编辑的答案当计时器停止时,我如何启动另一种方法?问题是,传统的倒计时计时器有很多错误,比如跳过最后一个滴答声,这就是为什么我为它创建了一个类。跳过最后一个滴答声是什么意思?如果你指的是时间等于零的最后一个滴答声,那么你可以写(millisUntilFinished/1000!=0){//do stuff}感谢您的帮助,但我不想添加新的倒计时计时器,我想使用timer类中的倒计时计时器,但使用on finish方法计时器停止时如何启动另一个方法?
public class countdown_timer extends CountDownTimer {
TextView textView;
@Override
public void onTick(long millisInFuture) {
long sec = millisInFuture/1000;
if(millisInFuture <= 0) {
Log.v("status", "done");
} else {
Log.v("status", Long.toString(sec) + " seconds remain and timer has stopped!");
}
}
@Override
public void onFinish() {
if(textView != null){
// change text in your textview
}
}
public countdown_timer(long pMillisInFuture, long pCountDownInterval) {
super(pMillisInFuture, pCountDownInterval);
}
public countdown_timer(TextView textView, long pMillisInFuture, long pCountDownInterval) {
super(pMillisInFuture, pCountDownInterval);
this.textView = textView;
}
}
public class MainActivity extends AppCompatActivity {
TextView textView;
CountDownTimer mycounterdown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
long startcard = 10000;
textView = (TextView) findViewById(R.id.test);
mycounterdown = new CountDownTimer(startcard, 1000) {
@Override
public void onTick(long mycounterup) {
int minutes_up_start = (int) (mycounterup / 1000) / 60;
int seconds_up_start = (int) (mycounterup / 1000) % 60;
String time_2_up_start_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_up_start, seconds_up_start);
textView.setText(time_2_up_start_formatted);
}
@Override
public void onFinish() {
// call here other methods from activity
testMethod();
}
};
mycounterdown.start();
}
public void testMethod(){
Toast.makeText(MainActivity.this, "Test Method called", Toast.LENGTH_SHORT).show();
}
}
public class MainActivity extends AppCompatActivity {
TextView textView;
CountDownTimer mycounterdown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
long startcard = 10000;
textView = (TextView) findViewById(R.id.test);
mycounterdown = new CountDownTimer(startcard, 500) {
@Override
public void onTick(long mycounterup) {
int minutes_up_start = (int) (mycounterup / 1000) / 60;
int seconds_up_start = (int) (mycounterup / 1000) % 60;
String time_2_up_start_formatted = String.format(Locale.getDefault(), "%02d:%02d", minutes_up_start, seconds_up_start);
textView.setText(time_2_up_start_formatted);
}
@Override
public void onFinish() {
// call here other methods from activity
testMethod();
}
};
mycounterdown.start();
}
public void testMethod(){
Toast.makeText(MainActivity.this, "Test Method called", Toast.LENGTH_SHORT).show();
}
}