如何从类没有延伸活动显示敬酒消息消息

2023-09-12 22:12:55 作者:伤心的眼泪谁来安慰

可能重复:   How做我做从非活性类举杯?

我如何创建并显示一个吐司消息不延长活动类的类?我在由活动

How can I create and show a Toast message from a class which does not extended the Activity class? I'm using this class in another class that is extended by Activity.

推荐答案

您需要一个背景参考。您可以在创建类明确作为参数传递

You need a context Reference. you can explicit pass as parameter when you create your class

public class MyClass {

  private static Context context;
  public MyClass(Context c) {
     context = c;
  }

  public static void showToastMethod() {
        Toast.makeText(context, "mymessage ", Toast.LENGTH_SHORT).show();
  }

}