android 支持自定义布局、线程安全、避免内存泄漏的 Toast 工具类

ops/2025/3/10 21:09:14/

支持自定义布局:可以灵活地显示自定义样式的 Toast

线程安全:确保在主线程中显示 Toast,避免崩溃。

避免内存泄漏:使用 ApplicationContext 和取消机制,防止内存泄漏问题。

工具类:作为一个通用的工具类,方便在项目中复用。

ToastUtil

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;public class ToastUtil {private static Toast toast; // 全局Toast对象,避免重复创建private static final int DEFAULT_GRAVITY = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL; // 默认显示位置private static final int DEFAULT_Y_OFFSET = 100; // 默认Y轴偏移量private static final Handler mainHandler = new Handler(Looper.getMainLooper()); // 主线程Handler/*** 显示短时间的Toast** @param context 上下文* @param message 要显示的消息*/public static void showShort(Context context, String message) {showToast(context, message, Toast.LENGTH_SHORT, DEFAULT_GRAVITY, 0, DEFAULT_Y_OFFSET);}/*** 显示长时间的Toast** @param context 上下文* @param message 要显示的消息*/public static void showLong(Context context, String message) {showToast(context, message, Toast.LENGTH_LONG, DEFAULT_GRAVITY, 0, DEFAULT_Y_OFFSET);}/*** 显示短时间的Toast(使用字符串资源ID)** @param context 上下文* @param resId   字符串资源ID*/public static void showShort(Context context, int resId) {showShort(context, context.getString(resId));}/*** 显示长时间的Toast(使用字符串资源ID)** @param context 上下文* @param resId   字符串资源ID*/public static void showLong(Context context, int resId) {showLong(context, context.getString(resId));}/*** 显示自定义位置的Toast** @param context  上下文* @param message  要显示的消息* @param gravity  显示位置(例如 Gravity.TOP)* @param xOffset  X轴偏移量* @param yOffset  Y轴偏移量*/public static void showAtPosition(Context context, String message, int gravity, int xOffset, int yOffset) {showToast(context, message, Toast.LENGTH_SHORT, gravity, xOffset, yOffset);}/*** 显示自定义布局的Toast** @param context     上下文* @param layoutResId 自定义布局资源ID* @param message     要显示的消息*/public static void showCustom(Context context, int layoutResId, String message) {runOnUiThread(() -> {if (toast != null) {toast.cancel(); // 取消之前的Toast}// 使用ApplicationContext,避免内存泄漏Context appContext = context.getApplicationContext();LayoutInflater inflater = (LayoutInflater) appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);View layout = inflater.inflate(layoutResId, null);// 查找布局中的TextView(假设id为text)TextView textView = layout.findViewById(R.id.text);if (textView != null) {textView.setText(message);}toast = new Toast(appContext);toast.setDuration(Toast.LENGTH_SHORT);toast.setView(layout);toast.show();});}/*** 显示自定义布局的Toast(支持自定义显示时长)** @param context     上下文* @param layoutResId 自定义布局资源ID* @param message     要显示的消息* @param duration    显示时长(Toast.LENGTH_SHORT 或 Toast.LENGTH_LONG)*/public static void showCustom(Context context, int layoutResId, String message, int duration) {runOnUiThread(() -> {if (toast != null) {toast.cancel(); // 取消之前的Toast}// 使用ApplicationContext,避免内存泄漏Context appContext = context.getApplicationContext();LayoutInflater inflater = (LayoutInflater) appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);View layout = inflater.inflate(layoutResId, null);// 查找布局中的TextView(假设id为text)TextView textView = layout.findViewById(R.id.text);if (textView != null) {textView.setText(message);}toast = new Toast(appContext);toast.setDuration(duration);toast.setView(layout);toast.show();});}/*** 核心方法:显示Toast** @param context  上下文* @param message  要显示的消息* @param duration 显示时长(Toast.LENGTH_SHORT 或 Toast.LENGTH_LONG)* @param gravity  显示位置* @param xOffset  X轴偏移量* @param yOffset  Y轴偏移量*/private static void showToast(Context context, String message, int duration, int gravity, int xOffset, int yOffset) {runOnUiThread(() -> {if (toast != null) {toast.cancel(); // 取消之前的Toast}// 使用ApplicationContext,避免内存泄漏Context appContext = context.getApplicationContext();toast = Toast.makeText(appContext, message, duration);toast.setGravity(gravity, xOffset, yOffset); // 设置显示位置toast.show();});}/*** 取消Toast*/public static void cancelToast() {if (toast != null) {toast.cancel();toast = null; // 释放引用}}/*** 确保在主线程中运行** @param runnable 需要执行的任务*/private static void runOnUiThread(Runnable runnable) {if (Looper.myLooper() == Looper.getMainLooper()) {runnable.run(); // 当前是主线程,直接运行} else {mainHandler.post(runnable); // 当前是子线程,切换到主线程运行}}
}

使用示例

  1. 显示自定义布局的 Toast
ToastUtil.showCustom(MainActivity.this, R.layout.custom_toast, "这是一个自定义Toast");

在子线程中调用:

new Thread(() -> {// 在子线程中调用ToastUtil.showCustom(MainActivity.this, R.layout.custom_toast, "子线程中的自定义Toast");
}).start();

自定义布局示例:
假设 res/layout/custom_toast.xml 是一个自定义布局文件,例如:

<!-- res/layout/custom_toast.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/toast_background"android:padding="16dp"android:orientation="horizontal"><ImageViewandroid:id="@+id/icon"android:layout_width="24dp"android:layout_height="24dp"android:src="@drawable/ic_toast_icon"android:layout_marginEnd="8dp"/><TextViewandroid:id="@+id/text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="@android:color/white"android:textSize="16sp"/>
</LinearLayout>

http://www.ppmy.cn/ops/164755.html

相关文章

mysql,docker一键创建以及链接报错:Public Key Retrieval is not allowed

创建&#xff1a; docker run --name mysql-test -e MYSQL_ROOT_PASSWORD123 -d -p 13306:3306 mysql:8.0.39 docker exec -it mysql-test /bin/bash mysql -u root -p 报错&#xff1a;Public Key Retrieval is not allowed

虚幻基础:动画层接口

文章目录 动画层&#xff1a;动画图表中的函数接口&#xff1a;名字&#xff0c;没有实现。动画层接口&#xff1a;由动画蓝图实现1.动画层可直接调用实现功能2.动画层接口必须安装3.动画层默认使用本身实现4.动画层也可使用其他动画蓝图实现&#xff0c;但必须在角色蓝图中关联…

在 Linux 下,服务器如何知道某个 TCP 连接来了消息? 这就涉及 IO 事件通知机制!

1. 阻塞 read() 阻塞 read() 就像你打电话问饭好了吗&#xff0c;但如果对方不接电话&#xff08;没数据&#xff09;&#xff0c;你就一直守在电话边等&#xff0c;什么别的事情都做不了。 你打电话问外卖员“饭好了吗&#xff1f;”&#xff0c;如果外卖还没送到&#xff0c…

nginx中upstream模块的使用。

upstream模块中的轮询算法 作用&#xff1a;将多台web服务器定义成一个upstream组&#xff0c;实现web服务的负载均衡。 后台的多个网站服务器&#xff0c;可以是多个不同的机器&#xff0c;也可以是多个不同的主机。 第一点&#xff1a;nginx在接受到客户端的访问请求后&am…

快手,蓝禾,得物,优博讯,三七互娱,顺丰,oppo,游卡,汤臣倍健,康冠科技,作业帮25届春招内推

快手&#xff0c;蓝禾&#xff0c;得物&#xff0c;优博讯&#xff0c;三七互娱&#xff0c;顺丰&#xff0c;oppo&#xff0c;游卡&#xff0c;汤臣倍健&#xff0c;康冠科技&#xff0c;作业帮25届春招内推 ①快手 【在招岗位】运营、市场、产品、战略分析、职能、工程、设计…

Elasticsearch如何删除字段

文章目录 1、问题描述2、案例讲解3、问题剖析4、解决方案4.1 注意&#xff1a;4.2 步骤&#xff1a;4.3 示例&#xff1a;4.3.1 创建一个新的索引4.3.2 reindex 重建索引4.3.3 恢复索引 5、彩蛋 & 坑5.1 坑5.2 使用 Pipline 6、总结 1、问题描述 在 Elasticsearch 中&…

uniApp实战二:仿今日相机水印功能

文章目录 1.最终效果预览2.页面实现 1.最终效果预览 2.页面实现 页面布局 <view click"handleTakePhotoNew">拍照</view><view v-if"!snapSrc"><canvas canvas-id"canvas" :style"{width:cvWidthpx,height:cvHeight…

利用可变参数模板,可打印任意参数和参数值。(C++很好的调式函数)

很酷的应用&#xff1a; &#xff08;1&#xff09; 如何获取可变参数名 代码例子&#xff1a; #define _test(...) (test_t(#__VA_ARGS__, __VA_ARGS__))template<typename... Args> void test_t(const char* names, Args... args) {std::cout << names <<…