gpt4 book ai didi

android - 在一项 Activity 中 toast 并在另一项 Activity 中展示

转载 作者:行者123 更新时间:2023-11-29 15:18:28 25 4
gpt4 key购买 nike

我有一个类扩展倒计时

我想在 onfinish() 中 toast ,每次从它实例化的任何对象中的任何其他类调用 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

}

}

最佳答案

您需要将上下文传递给 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();

关于android - 在一项 Activity 中 toast 并在另一项 Activity 中展示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21577456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com