封装的吐司的工具类
public class ToastUtils {
private static final String TAG = ToastUtils.class.getSimpleName();
private static Toast mCustomToast;
private static Toast mToast;
private static TextView mText;
private ToastUtils(){};
public static void showCustom(Context context, String text) {
Resources resources = context.getResources();
int iconId = 0;
if (text.contains("失败")) {
iconId = resources.getIdentifier("ic_toast_error", "drawable", context.getPackageName());
} else {
iconId = resources.getIdentifier("ic_tick", "drawable", context.getPackageName());
}
showCustom(context, iconId, text);
}
public static void showCustom(Context context, int iconId, String text) {
Resources resources = context.getResources();
if(mCustomToast == null) {
LayoutInflater inflater = LayoutInflater.from(context);
int resId = resources.getIdentifier("toast_view", "layout", context.getPackageName());
if (resId == 0) {
Log.e(TAG, "cannot find layout toast_view!");
}
View view = inflater.inflate(resId, null);
int textId = resources.getIdentifier("text", "id", context.getPackageName());
mText = (TextView)view.findViewById(textId);
mCustomToast = new Toast(context);
mCustomToast.setGravity(Gravity.CENTER, 0, 0);
mCustomToast.setView(view);
}
mText.setCompoundDrawablesWithIntrinsicBounds(0, iconId, 0, 0);
mText.setText(text);
mCustomToast.show();
}
public static void show(Context context, String text) {
show(context, text, false);
}
public static void show(Context context, String text, boolean isLongTime) {
if(mToast == null) {
mToast = Toast.makeText(context, text, isLongTime == true ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
} else {
mToast.setText(text);
mToast.setDuration(isLongTime == true ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT);
}
mToast.show();
}
}
Toast工具类
最新推荐文章于 2024-04-12 14:47:11 发布