Android各种好看吐司设计
2018年07月08日 10:44:59 峥嵘life 阅读数:649
版权声明:本文为博主原创文章。只要评论中留言就可以转载。 https://blog.csdn.net/wenzhi20102321/article/details/80957455
Android各种吐司设计
这里給大家介绍一下,几种简单的吐司的设计,虽然设计不难,但是有某些app界面显示比较好看。
效果:
这里设计了五种不同时刻需要的吐司,分别是
1、普通吐司
2、信息提示吐司
3、成功提示吐司
4、警告提示吐司
5、成功提示吐司
还有一些其他功能的吐司,比如有些页面需要三秒内两次后退为退出页面等等的提示。
这里把几个吐司的的使用封装在一个工具类中,需要的可以拿来使用就可以了。
这个封装的吐司类,有几个优点:
1、吐司的样式好看
2、调用方便
3、吐司的时间可控,并且下一个吐司会直接覆盖上一个吐司,
系统的吐司是等上一个吐司完成后在显示下一个吐司,这样如果多次点击吐司会弹很久的吐司
4、退出程序吐司会关闭
一.这里給大家看看上面显示页面调用的代码:
使用工具类的方法就是,先注册,后使用就可以了。
package com.example.wenzhi.dialog;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//init Toast ,最好是在Application中初始化,在所有Activity中都可以用!
//后面修改了一下,使用Activity对象获取了Application的上下文,所以任何一个页面注册,其他页面都是可以使用
RxToast.setContext(this);
}
//普通吐司
public void commonToast(View view) {
RxToast.normal("普通吐司");
//等同
// RxToast.normal("普通吐司", Toast.LENGTH_SHORT);
//默认是没有图标的,如果想加图标也可以
//RxToast.normal("普通吐司", Toast.LENGTH_SHORT,getDrawable( R.mipmap.ic_launcher));
}
//信息提示吐司
public void infoToast(View view) {
RxToast.info("信息提示吐司");
//等同,第一个参数是显示的信息,第二个参数是显示的时间,第三个参数是是否显示图标。
// RxToast.info("信息提示吐司", Toast.LENGTH_SHORT);
// RxToast.info("信息提示吐司",Toast.LENGTH_SHORT,true).show();
}
//成功提示吐司
public void successToast(View view) {
RxToast.success("成功提示吐司");
//等同
// RxToast.successToast("成功提示吐司",Toast.LENGTH_SHORT);
// RxToast.successToast("成功提示吐司",Toast.LENGTH_SHORT,true).show();
}
//警告提示吐司
public void warnToast(View view) {
RxToast.warning("警告提示吐司");
//等同
// RxToast.warnToast("警告提示吐司",Toast.LENGTH_SHORT);
// RxToast.warnToast("警告提示吐司",Toast.LENGTH_SHORT,true).show();
}
//错误提示吐司
public void errorToast(View view) {
RxToast.error("错误提示吐司");
//等同
// RxToast.errorToast("错误提示吐司",Toast.LENGTH_SHORT);
// RxToast.errorToast("错误提示吐司",Toast.LENGTH_SHORT,true).show();
}
//跳转到另一个页面
public void jumpActivity(View view) {
startActivity(new Intent(this, SecondActivity.class));
}
//提示两次返回退出程序
public void doubleClickToast(View view) {
boolean d = RxToast.doubleClickExit();
if (d) {
finish();
}
}
}
二.吐司工具类的代码:
工具类的几个方法,看看几个public修饰的方法就知道有哪几个了。
package com.example.wenzhi.dialog;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.os.Build;
import android.support.annotation.CheckResult;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
/**
* @author liwenzhi Toast Utils
*/
@SuppressLint("InflateParams")
public class RxToast {
@ColorInt
private static final int DEFAULT_TEXT_COLOR = Color.parseColor("#FFFFFF");
@ColorInt
private static final int ERROR_BACKGROUND_COLOR = Color.parseColor("#FD4C5B");
@ColorInt
private static final int INFO_BACKGROUND_COLOR = Color.parseColor("#3F51B5");
@ColorInt
private static final int SUCCESS_BACKGROUND_COLOR = Color.parseColor("#388E3C");
@ColorInt
private static final int WARNING_BACKGROUND_COLOR = Color.parseColor("#FFA900");
private static final String TOAST_TYPEFACE = "sans-serif-condensed";
private static Toast currentToast;
private static Toast mToast;
private static Context mContext;
private static long mExitTime;
private static Context getContext() {
return mContext;
}
public static void setContext(Context mContext) {
RxToast.mContext = mContext.getApplicationContext();
}
//normal
public static void normal(@NonNull String message) {
normal(getContext(), message, Toast.LENGTH_SHORT, null, false).show();
}
public static void normal(@NonNull String message, int duration) {
normal(getContext(), message, duration, null, false).show();
}
public static void normal(@NonNull String message, Drawable icon) {
normal(getContext(), message, Toast.LENGTH_SHORT, icon, true).show();
}
public static void normal(@NonNull String message, int duration, Drawable icon) {
normal(getContext(), message, duration, icon, true).show();
}
//warning
public static void warning(@NonNull String message) {
warning(getContext(), message, Toast.LENGTH_SHORT, true).show();
}
public static void warning(@NonNull String message, int duration) {
warning(getContext(), message, duration, true).show();
}
public static Toast warning(@NonNull String message, int duration, boolean withIcon) {
return custom(getContext(), message, getDrawable(getContext(), R.mipmap.ic_error_outline_white_48dp), DEFAULT_TEXT_COLOR, WARNING_BACKGROUND_COLOR, duration, withIcon, true);
}
//info
public static void info(@NonNull String message) {
info(getContext(), message, Toast.LENGTH_SHORT, true).show();
}
public static void info(@NonNull String message, int duration) {
info(getContext(), message, duration, true).show();
}
public static Toast info(@NonNull String message, int duration, boolean withIcon) {
return custom(getContext(), message, getDrawable(getContext(), R.mipmap.ic_info_outline_white_48dp), DEFAULT_TEXT_COLOR, INFO_BACKGROUND_COLOR, duration, withIcon, true);
}
//success
public static void success(@NonNull String message) {
success(getContext(), message, Toast.LENGTH_SHORT, true).show();
}
public static void success(@NonNull String message, int duration) {
success(getContext(), message, duration, true).show();
}
public static Toast success(@NonNull String message, int duration, boolean withIcon) {
return custom(getContext(), message, getDrawable(getContext(), R.mipmap.ic_check_white_48dp), DEFAULT_TEXT_COLOR, SUCCESS_BACKGROUND_COLOR, duration, withIcon, true);
}
//error
public static void error(@NonNull String message) {
error(getContext(), message, Toast.LENGTH_SHORT, true).show();
}
public static void error(@NonNull String message, int duration) {
error(getContext(), message, duration, true).show();
}
public static Toast error(@NonNull String message, int duration, boolean withIcon) {
return custom(getContext(), message, getDrawable(getContext(), R.mipmap.ic_clear_white_48dp), DEFAULT_TEXT_COLOR, ERROR_BACKGROUND_COLOR, duration, withIcon, true);
}
//******************************************simple Toast **************************************
/**
* simple Toast : need waiting
*/
public static void showToastShort(String str) {
Toast.makeText(getContext(), str, Toast.LENGTH_SHORT).show();
}
public static void showToastLong(String str) {
Toast.makeText(getContext(), str, Toast.LENGTH_LONG).show();
}
/**
*simple Toast : show rightNow
*/
public static void showToast(String msg) {
if (mToast == null) {
mToast = Toast.makeText(getContext(), msg, Toast.LENGTH_LONG);
} else {
mToast.setText(msg);
}
mToast.show();
}
/**
* click two time for exit
*/
public static boolean doubleClickExit() {
if ((System.currentTimeMillis() - mExitTime) > 2000) {
RxToast.normal("再按一次退出");
mExitTime = System.currentTimeMillis();
return false;
}
return true;
}
//*******************************************private method start********************************************
@CheckResult
private static Toast normal(@NonNull Context context, @NonNull String message, int duration, Drawable icon, boolean withIcon) {
return custom(context, message, icon, DEFAULT_TEXT_COLOR, duration, withIcon);
}
@CheckResult
private static Toast warning(@NonNull Context context, @NonNull String message, int duration, boolean withIcon) {
return custom(context, message, getDrawable(context, R.mipmap.ic_error_outline_white_48dp), DEFAULT_TEXT_COLOR, WARNING_BACKGROUND_COLOR, duration, withIcon, true);
}
@CheckResult
private static Toast info(@NonNull Context context, @NonNull String message, int duration, boolean withIcon) {
return custom(context, message, getDrawable(context, R.mipmap.ic_info_outline_white_48dp), DEFAULT_TEXT_COLOR, INFO_BACKGROUND_COLOR, duration, withIcon, true);
}
@CheckResult
private static Toast success(@NonNull Context context, @NonNull String message, int duration, boolean withIcon) {
return custom(context, message, getDrawable(context, R.mipmap.ic_check_white_48dp), DEFAULT_TEXT_COLOR, SUCCESS_BACKGROUND_COLOR, duration, withIcon, true);
}
@CheckResult
private static Toast error(@NonNull Context context, @NonNull String message, int duration, boolean withIcon) {
return custom(context, message, getDrawable(context, R.mipmap.ic_clear_white_48dp), DEFAULT_TEXT_COLOR, ERROR_BACKGROUND_COLOR, duration, withIcon, true);
}
@CheckResult
private static Toast custom(@NonNull Context context, @NonNull String message, Drawable icon, @ColorInt int textColor, int duration, boolean withIcon) {
return custom(context, message, icon, textColor, -1, duration, withIcon, false);
}
@CheckResult
private static Toast custom(@NonNull Context context, @NonNull String message, @DrawableRes int iconRes, @ColorInt int textColor, @ColorInt int tintColor, int duration, boolean withIcon, boolean shouldTint) {
return custom(context, message, getDrawable(context, iconRes), textColor, tintColor, duration, withIcon, shouldTint);
}
@CheckResult
private static Toast custom(@NonNull Context context, @NonNull String message, Drawable icon, @ColorInt int textColor, @ColorInt int tintColor, int duration, boolean withIcon, boolean shouldTint) {
if (currentToast == null) {
currentToast = new Toast(context);
}
final View toastLayout = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.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 = tint9PatchDrawableFrame(context, tintColor);
} else {
drawableFrame = getDrawable(context, R.mipmap.toast_frame);
}
setBackground(toastLayout, drawableFrame);
if (withIcon) {
if (icon == null)
throw new IllegalArgumentException("Avoid passing 'icon' as null if 'withIcon' is set to true");
setBackground(toastIcon, icon);
} else
toastIcon.setVisibility(View.GONE);
toastTextView.setTextColor(textColor);
toastTextView.setText(message);
toastTextView.setTypeface(Typeface.create(TOAST_TYPEFACE, Typeface.NORMAL));
currentToast.setView(toastLayout);
currentToast.setDuration(duration);
return currentToast;
}
private static final Drawable tint9PatchDrawableFrame(@NonNull Context context, @ColorInt int tintColor) {
final NinePatchDrawable toastDrawable = (NinePatchDrawable) getDrawable(context, R.mipmap.toast_frame);
toastDrawable.setColorFilter(new PorterDuffColorFilter(tintColor, PorterDuff.Mode.SRC_IN));
return toastDrawable;
}
private static final void setBackground(@NonNull View view, Drawable drawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
view.setBackground(drawable);
else
view.setBackgroundDrawable(drawable);
}
private static final 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);
}
//===========================================private method end============================================
}
上面工具类的代码理解也是不难的,想用可以直接copy过去,但是这里是有几个小图标和一个背景图片的!
对于吐司工具类,不满意的可以进行适当修改,符合自己的使用习惯就好。
这个吐司示例的代码:https://download.csdn.net/download/wenzhi20102321/10528056
如果没有csdn积分,就在Git中下载:https://github.com/liwenzhi/WZToast/tree/master