我有一个与我的 Activity 分开的类,并且我希望当从该类调用方法时在该 Activity 中出现一个 toast。我怎么做?
这是类中的方法。
我希望在 else if (counter < addSpend)
中调用 toast声明。
public void shipAdd()
{
if(counter >= addSpend)
{
counter -= addSpend;
new TimerClass(addTime,1000)
{
public void onFinish() {
counter += addAmount;
this.start();
}
}.start();
}else if (counter < addSpend)
{
}
}
最佳答案
找到一种方法来获取 Activity 的上下文。我建议将其作为参数传递给 shipAdd 方法。然后像往常一样 Toast.makeText(context, message,uration).show()
。
关于java - 如何为不同类(class)内的 Activity 创建 toast ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25824560/