我有一个类扩展countdowntimer
我想在onfinish()中做一个祝酒词,每当从任何对象实例化的任何其他类调用onfinish()函数时都会显示
我该怎么做?
package com.fawzyx.exams_countdowntimer;
import android.os.CountDownTimer;
import android.text.InputFilter.LengthFilter;
import android.widget.Toast;
public class CountDown extends CountDownTimer {
public CountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
/*
millisInFuture : The number of millis in the future from the call to start() until the countdown is done and onFinish() is called.
countDownInterval : The interval along the way to receive onTick(long) callbacks.
*/
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext() ,"Done", Toast.LENGTH_LONG);
}
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:2)
您需要将上下文传递给CounteDown类
new CountDown(MyActivityName.this,otherparams);
现在使用传递的上下文来显示toast
Context context;
public CountDown(Context context,long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.context = context;
}
然后在onFinish()
Toast.makeText(context," Message", Toast.LENGTH_SHORT).show();
答案 1 :(得分:1)
试试这个:
public class CountDown extends CountDownTimer {
private Context context;
public CountDown(Context context, long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
this.context = context;
}
@Override
public void onFinish() {
Toast.makeText(context ,"Done", Toast.LENGTH_LONG).show();
}
}
答案 2 :(得分:1)
两个问题:
您需要传递Context
,例如作为构造函数arg:
private Context mContext;
public CountDown(Context context, long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
mContext = context;
将此mContext
传递给Toast
。
在Toast.makeText()
之后,请致电show()
进行实际展示。
答案 3 :(得分:1)
像这样使用:
public class CountDown extends CountDownTimer {
Context context;
public CountDown(long millisInFuture, long countDownInterval , Context ctx) {
super(millisInFuture, countDownInterval);
this.context = ctx;
/*
millisInFuture : The number of millis in the future from the call to start() until the countdown is done and onFinish() is called.
countDownInterval : The interval along the way to receive onTick(long) callbacks.
*/
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
Toast.makeText(context ,"Done", Toast.LENGTH_LONG);
}
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
}
}
在调用construtor
上下文时调用其Activity
,无论我们想要倒计时等级
答案 4 :(得分:0)
更简单的方法是覆盖onStop
并使用getApplicationContext()
。然后在活动结束时出现吐司。
@Override
protected void onStop() {
super.onStop();
Toast toast = Toast.makeText(getApplicationContext(),"Toast message here",Toast.LENGTH_SHORT);
toast.show();
}