安卓Toast自定义及防止重复显示

一、简介

Toast是Android系统常用的一种提示信息方式,通常用于显示短暂且简短的提示信息,比如登录成功、网络连接错误等。系统默认Toast可以实现简单效果,但通常情况下需要自定义Toast,以便产品界面与设计要求相符。本文将介绍如何自定义Toast及防止Toast的重复显示。

二、自定义Toast

系统默认Toast样式不难看,但也不与产品的设计要求相符,因此通常需要对Toast进行自定义。

1. 创建布局文件toast.xml

在Layout文件夹下新建一个命名为toast.xml的布局文件,如下所示:

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/bg_toast"

android:orientation="horizontal"

android:padding="10dp">

android:id="@+id/iv_icon"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginRight="5dp"

android:src="@drawable/ic_toast" />

android:id="@+id/tv_content"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textColor="@android:color/white"

android:textSize="14sp" />

其中,LinearLayout中设置了背景和内边距等属性,ImageView是Toast中的图片,TextView是要显示的内容。以上只是一个简单的自定义样式,可以根据需要进行修改。

2. 创建自定义Toast

接下来在Activity中显示自定义的Toast,创建一个自定义的Toast类来实现,如下所示:

public class CustomToast {

private Toast mToast;

/**

* 显示文本Toast

*

* @param context 上下文对象

* @param content 显示的内容

* @param duration 显示时间

*/

public void showToast(Context context, String content, int duration) {

if (mToast == null) {

mToast = new Toast(context);

}

View view = LayoutInflater.from(context).inflate(R.layout.toast, null);

TextView textView = view.findViewById(R.id.tv_content);

ImageView imageView = view.findViewById(R.id.iv_icon);

textView.setText(content);

imageView.setImageResource(R.drawable.ic_toast);

mToast.setView(view);

mToast.setDuration(duration);

mToast.show();

}

}

其中,showToast()方法用来显示Toast,并分别获取自定义的布局文件中的ImageView和TextView,并设置显示的内容,ImageView需要传入图片资源。最终的样式如下图所示:

![Toast样式](https://img-blog.csdnimg.cn/20201206195431677.png)

三、防止Toast的重复显示

在Android中经常会遇到Toast重复显示的情况,此时需要做出一些处理,防止Toast不能及时消失。实现防止Toast重复显示的方法有很多,本文将介绍两种方法。

1. 使用Handler+Runnable方式

在显示Toast之前,使用Handler、Runnable方式检测之前是否有Toast显示,如果有,则使用cancel()方法取消Toast的显示。

private int mLastToastHashCode; //保存上个Toast的HashCode

/***

* 显示 Toast

* @param context

* @param content

* @param duration 显示时间

*/

public void showToast(Context context, String content, int duration) {

if (mToast == null) {

mToast = new Toast(context);

}

View view = LayoutInflater.from(context).inflate(R.layout.toast, null);

TextView textView = view.findViewById(R.id.tv_content);

ImageView imageView = view.findViewById(R.id.iv_icon);

textView.setText(content);

imageView.setImageResource(R.drawable.ic_toast);

// 判断上一个Toast是否已经消失,如果没有,则取消上一个Toast

if (mLastToastHashCode != 0 && mLastToastHashCode == mToast.hashCode()) {

mToast.cancel();

}

mLastToastHashCode = mToast.hashCode();

mToast.setView(view);

mToast.setDuration(duration);

mToast.show();

// 延时取消Toast,防止Toast重复显示 不传递延时时间的情况下默认使用 LENGTH_SHORT 的时间

new Handler().postDelayed(new Runnable() {

@Override

public void run() {

mToast.cancel();

}

}, duration == Toast.LENGTH_SHORT ? 2000 : 3500);

}

2. 使用ToastUtil方式实现

使用单例ToastUtil方式,将上一次的时间和内容记录下来,之后判断是否与本次相同,在相同的情况下只更新显示时间。

public class ToastUtil {

private static ToastUtil sInstance;

private Toast mToast;

private String mLastContent;

private long mLastTime;

public static ToastUtil getInstance() {

if (sInstance == null) {

sInstance = new ToastUtil();

}

return sInstance;

}

public void showToast(Context context, String content, int duration) {

if (TextUtils.isEmpty(content)) {

return;

}

if (mToast == null) {

mToast = Toast.makeText(context.getApplicationContext(), content, duration);

} else {

if (!TextUtils.isEmpty(mLastContent) && content.equals(mLastContent)) {

duration = (int) (System.currentTimeMillis() - mLastTime) > duration ? duration : (int) (System.currentTimeMillis() - mLastTime);

mToast.setDuration(duration);

} else {

mToast.setText(content);

mToast.setDuration(duration);

}

}

mToast.show();

mLastContent = content;

mLastTime = System.currentTimeMillis();

}

}

通过上面两种方法,可以有效防止Toast的重复显示。

四、案例

1. 带图片的Toast显示

CustomToast customToast = new CustomToast();

customToast.showToast(MainActivity.this, "自定义Toast", Toast.LENGTH_SHORT);

2. 带文字的Toast显示

ToastUtil.getInstance().showToast(MainActivity.this, "带图片的Toast", Toast.LENGTH_SHORT);

以上就是安卓Toast自定义及防止重复显示的详细介绍。自定义Toast是在产品设计上必须要实现的一个功能,而防止Toast重复显示也是一个很常见的问题。此文通过案例演示了两种方法,对于Android开发工程师来说是非常实用的。 如果你喜欢我们三七知识分享网站的文章, 欢迎您分享或收藏知识分享网站文章 欢迎您到我们的网站逛逛喔!https://www.37seo.cn/

点赞(17) 打赏

评论列表 共有 0 条评论

暂无评论
立即
投稿
发表
评论
返回
顶部