/**
* 单例 Toast 工具类
*/
public class ToastUtil {
private static Toast toast_default;
private static Toast toast_custom;
private ToastUtil() {
}
/**
* 显示 普通的 toast
*
* @param msg
*/
public static void showToast(String msg) {
if (toast_custom != null){
toast_custom.cancel();
}
if (toast_default == null) {
toast_default = Toast.makeText(MyApplication.context, msg, Toast.LENGTH_SHORT);
} else {
toast_default.setText(msg);
}
toast_default.show();
}
/**
* 显示在屏幕中间的 toast
*
* @param msg
*/
public static void showToastCenter(String msg) {
if (toast_custom != null){
toast_custom.cancel();
}
if (toast_default == null) {
toast_default = Toast.makeText(MyApplication.context, msg, Toast.LENGTH_SHORT);
} else {
toast_default.setText(msg);
}
toast_default.setGravity(Gravity.CENTER, 0, 0);
toast_default.show();
}
/**
* 显示自定义布局的 toast
*
* @param msg
*/
public static void showToastCustom(Context context, String msg) {
if (toast_default != null){
toast_default.cancel();
}
if (toast_custom == null) {
toast_custom = new Toast(context);
}
toast_custom.setView(View.inflate(MyApplication.context, R.layout.layout_toast, null));
TextView tv_msg = (TextView) toast_custom.getView().findViewById(R.id.tv_msg);
tv_msg.setText(msg);
toast_custom.setGravity(Gravity.CENTER, 0, 0);
toast_custom.show();
}
/**
* 是否仅在 debug 模式下显示
*
* @param msg
*/
public static void showToastDebug(String msg) {
if (!BuildConfig.DEBUG) {
return;
}
showToast(msg);
}
ToastUtil:Toast 工具类
最新推荐文章于 2024-04-28 14:00:09 发布