Snackbar、Toast工具类

2018-02-11 14:12:53来源:https://www.jianshu.com/p/5b06819450c1作者:天鬼人点击

分享


Snackbar工具类:


package com.hwc.oklib.util.toast;
import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.hwc.oklib.util.R;

/**
* 时间:2017/8/3
* 作者:黄伟才
* 简书:http://www.jianshu.com/p/87e7392a16ff
* github:https://github.com/huangweicai/OkLibDemo
* 描述:Snackbar使用工具类
*/
public class SnackbarUtil {
public static final int Info = 1;
public static final int Confirm = 2;
public static final int Warning = 3;
public static final int Alert = 4;
public static int red = 0xfff44336;
public static int green = 0xff4caf50;
public static int blue = 0xff2195f3;
public static int orange = 0xffffc107;
/**
* 短显示Snackbar,自定义颜色
* @param view
* @param message
* @param messageColor
* @param backgroundColor
* @return
*/
public static Snackbar ShortSnackbar(View view, String message, int messageColor, int backgroundColor) {
Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_SHORT);
setSnackbarColor(snackbar, messageColor, backgroundColor);
return snackbar;
}
/**
* 长显示Snackbar,自定义颜色
* @param view
* @param message
* @param messageColor
* @param backgroundColor
* @return
*/
public static Snackbar LongSnackbar(View view, String message, int messageColor, int backgroundColor) {
Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_LONG);
setSnackbarColor(snackbar, messageColor, backgroundColor);
return snackbar;
}
/**
* 自定义时常显示Snackbar,自定义颜色
* @param view
* @param message
* @param messageColor
* @param backgroundColor
* @return
*/
public static Snackbar IndefiniteSnackbar(View view, String message, int duration, int messageColor, int backgroundColor) {
Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_INDEFINITE).setDuration(duration);
setSnackbarColor(snackbar, messageColor, backgroundColor);
return snackbar;
}
/**
* 短显示Snackbar,可选预设类型
* @param view
* @param message
* @param type
* @return
*/
public static Snackbar ShortSnackbar(View view, String message, int type) {
Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_SHORT);
switchType(snackbar, type);
return snackbar;
}
/**
* 长显示Snackbar,可选预设类型
* @param view
* @param message
* @param type
* @return
*/
public static Snackbar LongSnackbar(View view, String message, int type) {
Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_LONG);
switchType(snackbar, type);
return snackbar;
}
/**
* 自定义时常显示Snackbar,可选预设类型
* @param view
* @param message
* @param type
* @return
*/
public static Snackbar IndefiniteSnackbar(View view, String message, int duration, int type) {
Snackbar snackbar = Snackbar.make(view, message, Snackbar.LENGTH_INDEFINITE).setDuration(duration);
switchType(snackbar, type);
return snackbar;
}
//选择预设类型
private static void switchType(Snackbar snackbar, int type) {
switch (type) {
case Info:
setSnackbarColor(snackbar, blue);
break;
case Confirm:
setSnackbarColor(snackbar, green);
break;
case Warning:
setSnackbarColor(snackbar, orange);
break;
case Alert:
setSnackbarColor(snackbar, Color.YELLOW, red);
break;
}
}
/**
* 设置Snackbar背景颜色 * @param snackbar * @param backgroundColor
*/
public static void setSnackbarColor(Snackbar snackbar, int backgroundColor) {
View view = snackbar.getView();
if (view != null) {
view.setBackgroundColor(backgroundColor);
}
}
/**
* 设置Snackbar文字和背景颜色
* @param snackbar
* @param messageColor
* @param backgroundColor
*/
public static void setSnackbarColor(Snackbar snackbar, int messageColor, int backgroundColor) {
View view = snackbar.getView();
if (view != null) {
view.setBackgroundColor(backgroundColor);
((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(messageColor);
}
}
/**
* 向Snackbar中添加view
* @param snackbar
* @param layoutId
* @param index 新加布局在Snackbar中的位置
*/
public static void SnackbarAddView(Snackbar snackbar, int layoutId, int index) {
View snackbarview = snackbar.getView();
Snackbar.SnackbarLayout snackbarLayout = (Snackbar.SnackbarLayout) snackbarview;
View add_view = LayoutInflater.from(snackbarview.getContext()).inflate(layoutId, null);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
p.gravity = Gravity.CENTER_VERTICAL;
snackbarLayout.addView(add_view, index, p);
}
}

Toast相关工具类,支持Toast多样式:


package com.hwc.oklib.util.toast;
import android.widget.Toast;
import com.hwc.oklib.util.UtilEntry;
/**
* 时间:2017/8/3
* 作者:黄伟才
* 简书:http://www.jianshu.com/p/87e7392a16ff
* github:https://github.com/huangweicai/OkLibDemo
* 描述:Toast工具类
*/
public class ToastUtil {
public static void success(CharSequence text) {
if (UtilEntry.isDebug)
if (null != UtilEntry.application)
Toasty.success(UtilEntry.application, text, Toast.LENGTH_SHORT, true).show();
}
public static void error(CharSequence text) {
if (UtilEntry.isDebug)
if (null != UtilEntry.application)
Toasty.error(UtilEntry.application, text, Toast.LENGTH_SHORT, true).show();
}
public static void info(CharSequence text) {
if (UtilEntry.isDebug)
if (null != UtilEntry.application)
Toasty.info(UtilEntry.application, text, Toast.LENGTH_SHORT, true).show();
}
public static void warn(CharSequence text) {
if (UtilEntry.isDebug)
if (null != UtilEntry.application)
Toasty.warning(UtilEntry.application, text, Toast.LENGTH_SHORT, true).show();
}
public static void show(CharSequence text) {
if (UtilEntry.isDebug)
if (null != UtilEntry.application)
Toasty.normal(UtilEntry.application, text, Toast.LENGTH_SHORT).show();
}
public static void show(CharSequence text, int resId) {
if (UtilEntry.isDebug)
if (null != UtilEntry.application)
Toasty.normal(UtilEntry.application, text, UtilEntry.application.getResources().getDrawable(resId)).show();
}
}

package com.hwc.oklib.util.toast;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.view.View;
import com.hwc.oklib.util.R;

/**
* This file is part of Toasty.
*
* Toasty is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Toasty is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Toasty. If not, see <http://www.gnu.org/licenses/>.
*/
final class ToastyUtils {
private ToastyUtils() {
}
static Drawable tint9PatchDrawableFrame(@NonNull Context context, @ColorInt int tintColor) {
final NinePatchDrawable toastDrawable = (NinePatchDrawable) getDrawable(context, R.drawable.oklib_toast_frame);
toastDrawable.setColorFilter(new PorterDuffColorFilter(tintColor, PorterDuff.Mode.SRC_IN));
return toastDrawable;
}
static void setBackground(@NonNull View view, Drawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
view.setBackground(drawable);
else
view.setBackgroundDrawable(drawable);
}
static Drawable getDrawable(@NonNull Context context, @DrawableRes int id) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
return context.getDrawable(id);
else
return context.getResources().getDrawable(id);
}
}

package com.hwc.oklib.util.toast;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.annotation.CheckResult;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.hwc.oklib.util.R;

/**
* This file is part of Toasty.
*
* Toasty is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Toasty is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Toasty. If not, see <http://www.gnu.org/licenses/>.
*/
@SuppressLint("InflateParams")
public class Toasty {
private static Toast mToast;
public static boolean isCenter = false;
private static final @ColorInt int DEFAULT_TEXT_COLOR = Color.parseColor("#FFFFFF");
private static final @ColorInt int ERROR_COLOR = Color.parseColor("#D50000");
private static final @ColorInt int INFO_COLOR = Color.parseColor("#3F51B5");
private static final @ColorInt int SUCCESS_COLOR = Color.parseColor("#388E3C");
private static final @ColorInt int WARNING_COLOR = Color.parseColor("#FFA900");
private static final String TOAST_TYPEFACE = "sans-serif-condensed";
private Toasty() {
}
public static @CheckResult Toast normal(@NonNull Context context, @NonNull CharSequence message) {
return normal(context, message, Toast.LENGTH_SHORT, null, false);
}
public static @CheckResult Toast normal(@NonNull Context context, @NonNull CharSequence message, Drawable icon) {
return normal(context, message, Toast.LENGTH_SHORT, icon, true);
}
public static @CheckResult Toast normal(@NonNull Context context, @NonNull CharSequence message, int duration) {
return normal(context, message, duration, null, false);
}
public static @CheckResult Toast normal(@NonNull Context context, @NonNull CharSequence message, int duration,
Drawable icon) {
return normal(context, message, duration, icon, true);
}
public static @CheckResult Toast normal(@NonNull Context context, @NonNull CharSequence message, int duration,
Drawable icon, boolean withIcon) {
return custom(context, message, icon, DEFAULT_TEXT_COLOR, duration, withIcon);
}
public static @CheckResult Toast warning(@NonNull Context context, @NonNull CharSequence message) {
return warning(context, message, Toast.LENGTH_SHORT, true);
}
public static @CheckResult Toast warning(@NonNull Context context, @NonNull CharSequence message, int duration) {
return warning(context, message, duration, true);
}
public static @CheckResult Toast warning(@NonNull Context context, @NonNull CharSequence message, int duration, boolean withIcon) {
return custom(context, message, ToastyUtils.getDrawable(context, R.drawable.oklib_ic_error_outline_white_48dp),
DEFAULT_TEXT_COLOR, WARNING_COLOR, duration, withIcon, true);
}
public static @CheckResult Toast info(@NonNull Context context, @NonNull CharSequence message) {
return info(context, message, Toast.LENGTH_SHORT, true);
}
public static @CheckResult Toast info(@NonNull Context context, @NonNull CharSequence message, int duration) {
return info(context, message, duration, true);
}
public static @CheckResult Toast info(@NonNull Context context, @NonNull CharSequence message, int duration, boolean withIcon) {
return custom(context, message, ToastyUtils.getDrawable(context, R.drawable.oklib_ic_info_outline_white_48dp),
DEFAULT_TEXT_COLOR, INFO_COLOR, duration, withIcon, true);
}
public static @CheckResult Toast success(@NonNull Context context, @NonNull CharSequence message) {
return success(context, message, Toast.LENGTH_SHORT, true);
}
public static @CheckResult Toast success(@NonNull Context context, @NonNull CharSequence message, int duration) {
return success(context, message, duration, true);
}
public static @CheckResult Toast success(@NonNull Context context, @NonNull CharSequence message, int duration, boolean withIcon) {
return custom(context, message, ToastyUtils.getDrawable(context, R.drawable.oklib_ic_check_white_48dp),
DEFAULT_TEXT_COLOR, SUCCESS_COLOR, duration, withIcon, true);
}
public static @CheckResult Toast error(@NonNull Context context, @NonNull CharSequence message) {
return error(context, message, Toast.LENGTH_SHORT, true);
}
public static @CheckResult Toast error(@NonNull Context context, @NonNull CharSequence message, int duration) {
return error(context, message, duration, true);
}
public static @CheckResult Toast error(@NonNull Context context, @NonNull CharSequence message, int duration, boolean withIcon) {
return custom(context, message, ToastyUtils.getDrawable(context, R.drawable.oklib_ic_clear_white_48dp),
DEFAULT_TEXT_COLOR, ERROR_COLOR, duration, withIcon, true);
}
public static @CheckResult Toast custom(@NonNull Context context, @NonNull CharSequence message, Drawable icon,
@ColorInt int textColor, int duration, boolean withIcon) {
return custom(context, message, icon, textColor, -1, duration, withIcon, false);
}
public static @CheckResult Toast custom(@NonNull Context context, @NonNull CharSequence message, @DrawableRes int iconRes,
@ColorInt int textColor, @ColorInt int tintColor, int duration,
boolean withIcon, boolean shouldTint) {
return custom(context, message, ToastyUtils.getDrawable(context, iconRes), textColor,
tintColor, duration, withIcon, shouldTint);
}
public static @CheckResult Toast custom(@NonNull Context context, @NonNull CharSequence message, Drawable icon,
@ColorInt int textColor, @ColorInt int tintColor, int duration,
boolean withIcon, boolean shouldTint) {
if (mToast ==null){
mToast = new Toast(context);
}
final View toastLayout = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.oklib_toast_layout, null);
final ImageView toastIcon = (ImageView) toastLayout.findViewById(R.id.toast_icon);
final TextView toastTextView = (TextView) toastLayout.findViewById(R.id.toast_text);
Drawable drawableFrame;
if (shouldTint)
drawableFrame = ToastyUtils.tint9PatchDrawableFrame(context, tintColor);
else
drawableFrame = ToastyUtils.getDrawable(context, R.drawable.oklib_toast_frame);
ToastyUtils.setBackground(toastLayout, drawableFrame);
if (withIcon) {
if (icon == null)
throw new IllegalArgumentException("Avoid passing 'icon' as null if 'withIcon' is set to true");
ToastyUtils.setBackground(toastIcon, icon);
} else
toastIcon.setVisibility(View.GONE);
toastTextView.setTextColor(textColor);
toastTextView.setText(message);
toastTextView.setTypeface(Typeface.create(TOAST_TYPEFACE, Typeface.NORMAL));
//mToast.setGravity(gravty);
if(isCenter){
mToast.setGravity(Gravity.CENTER,0,0);
}
mToast.setView(toastLayout);
mToast.setDuration(duration);
return mToast;
}
}







最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台