通过静态方法创建Toast [英] Create Toast by a static method
本文介绍了通过静态方法创建Toast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在所有应用程序片段中都使用此代码,如果我使用静态方法,效果会更好.我该怎么做?此静态方法也应适用于片段,而不仅适用于活动.
I use this code in all my app fragments and it should be better if I use a static method. How can I do it? This static method should also works on fragment, not only activities.
我的非静态showToast方法:
My not static showToast method:
public void showToast(String msg){
Toast.makeText(getActivity().getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}
使用这种静态方法解决了@@ KishanDhamat
SOLVED BY USING THIS STATIC METHOD thanks @KishanDhamat
public static void showToast(Context context, String text) {
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}
推荐答案
使用此:
public static void showToast(Context context, String text) {
Toast.makeText(context, text, Toast.LENGTH_LONG).show();
}
现在调用此方法时,您应该这样调用:
now for calling this method you should call like this:
ClassName.showToast(context,"text");
这里的类名是包含静态方法的类.
Here classname is class that containing static method.
这篇关于通过静态方法创建Toast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文