如何从类没有延伸活动显示敬酒消息 [英] How to display a Toast message in from a class that doesn't extend Activity

查看:109
本文介绍了如何从类没有延伸活动显示敬酒消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/5543769/how-do-i-make-a-toast-from-a-non-activity-class">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();
  }

}

这篇关于如何从类没有延伸活动显示敬酒消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆