解决方法有两个:
第一是: Toast.makeText()方法的第二个参数传null,然后mtoast.settext(text)重新设置一下。
第二是:自定义toast,ToastUtils类
//显示有应用名称的效果:
支付宝:你好,这是测试Toast内容!
//代码如下:
Toast.makeText(getActivity(), "你好,这是测试Toast内容!", Toast.LENGTH_LONG)
---------------------------------------
//显示无应用名称的效果:
你好,这是测试Toast内容!
//代码如下:Toast.makeText()方法的第二个参数传null,然后mtoast.settext(text)重新设置一下。
Toast mtoast= Toast.makeText(getActivity(), null, Toast.LENGTH_LONG);
toast.setText("你好,这是测试Toast内容!");
toast.show();
//ToastUtils方便调用
package cc.wulian.smarthomev6.support.utils;
import android.support.annotation.StringRes;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;
import cc.wulian.smarthomev6.R;
import cc.wulian.smarthomev6.main.application.MainApplication;
/**
* Created by huxc on 2017/6/15.
* 统一弹Toast
*/
public class ToastUtils {
private static Toast toast;
public static void show(String text) {
if (toast == null) {
toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_SHORT); //这个地方第二个参数需要为null
toast.setText(text);
} else {
toast.setText(text);
}
toast.show();
}
public static void show(@StringRes int resId) {
if (toast == null) {
toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_SHORT);
toast.setText(resId);
} else {
toast.setText(resId);
}
toast.show();
}
/**
* 弹出多个toast时, 不会一个一个的弹, 后面一个要显示的内容直接显示在当前的toast上
*/
public static void single(String msg) {
if (toast == null) {
toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_SHORT);
toast.setText(msg);
} else {
toast.setText(msg);
}
toast.show();
}
public static void singleLong(String msg) {
if (toast == null) {
toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_LONG);
toast.setText(msg);
} else {
toast.setText(msg);
}
toast.show();
}
/**
* 多行居中显示
*/
public static void singleCenter(@StringRes int msg) {
if (toast == null) {
toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_SHORT);
toast.setText(msg);
} else {
toast.setText(msg);
}
((TextView) toast.getView().findViewById(android.R.id.message)).setGravity(Gravity.CENTER);
toast.show();
}
/**
* 多行居中显示
*/
public static void singleCenter(String msg) {
if (toast == null) {
toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_SHORT);
toast.setText(msg);
} else {
toast.setText(msg);
}
((TextView) toast.getView().findViewById(android.R.id.message)).setGravity(Gravity.CENTER);
toast.show();
}
/**
* 弹出多个toast时, 不会一个一个的弹, 后面一个要显示的内容直接显示在当前的toast上
*/
public static void single(@StringRes int msg) {
if (toast == null) {
toast = Toast.makeText(MainApplication.getApplication(), null, Toast.LENGTH_SHORT);
toast.setText(msg);
} else {
toast.setText(msg);
}
toast.show();
}
}
06-29
1559
07-08
5232