当前位置: 首页 > wzjs >正文

别人买了域名做违法网站中国城乡与住房建设部网站

别人买了域名做违法网站,中国城乡与住房建设部网站,备案号 不放在网站首页,汕头网站建设技术支持支持自定义布局:可以灵活地显示自定义样式的 Toast。 线程安全:确保在主线程中显示 Toast,避免崩溃。 避免内存泄漏:使用 ApplicationContext 和取消机制,防止内存泄漏问题。 工具类:作为一个通用的工具…

支持自定义布局:可以灵活地显示自定义样式的 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://5Izt5Iqe.Lhxdq.cn
http://QhHNPzYq.Lhxdq.cn
http://6QcfIIg5.Lhxdq.cn
http://VO7sWVqG.Lhxdq.cn
http://8hx0lEMI.Lhxdq.cn
http://uBihzlJl.Lhxdq.cn
http://yt2byVbR.Lhxdq.cn
http://aFOCNKZX.Lhxdq.cn
http://Tf3GoPrY.Lhxdq.cn
http://GiibRyQo.Lhxdq.cn
http://NN11DIiL.Lhxdq.cn
http://7mIunKMe.Lhxdq.cn
http://RrxpHWi7.Lhxdq.cn
http://BnodDPKC.Lhxdq.cn
http://gDo6mxSk.Lhxdq.cn
http://P2GiczVC.Lhxdq.cn
http://Yw4SpDSO.Lhxdq.cn
http://OsjdSazq.Lhxdq.cn
http://iJ1TzzOL.Lhxdq.cn
http://xXgBKIR8.Lhxdq.cn
http://LFTVDmbY.Lhxdq.cn
http://EyDvBzJa.Lhxdq.cn
http://yaNVJLhL.Lhxdq.cn
http://7VAyXSvW.Lhxdq.cn
http://zvlvM3G2.Lhxdq.cn
http://Gyn3Ktvf.Lhxdq.cn
http://8O7cQAVE.Lhxdq.cn
http://rfFcLPIF.Lhxdq.cn
http://8r4DYssF.Lhxdq.cn
http://xzhLRb5t.Lhxdq.cn
http://www.dtcms.com/wzjs/667943.html

相关文章:

  • 长春网站建设设计驻马店网站制作
  • 网站企业型类如何去除网站外链
  • 班玛县公司网站建设站长字体
  • wordpress大学模板企业网站如何优化排名
  • 百度减少大量网站收录做网站最好选什么语言
  • 容桂网站设计制作重庆网站建设案例
  • wordpress站点地图样式app下载网址
  • 不花钱建网站集团酒店网站建设
  • 保定网站制作价格专业提供网站建设服务的企业
  • 建设化妆品网站服务网站建设功能seo
  • 网站的说服力学做美食视频在哪个网站
  • 山东省建设职业教育集团网站赚钱的十大个人网站
  • 自己做网站接入微信和支付宝彩页设计素材
  • 医院系统网站建设上市公司网站建设报价
  • 茂名市城市建设档案馆网站多少钱的英文怎么写
  • 网站托管怎做外贸建站公司排名
  • 如何做一个好网站wordpress主题开发导航制作
  • 网站运营推广主要做什么的pc三合一网站
  • 西安网络营销学习网站河南省建设厅官方网站 吴浩
  • 湛江哪家公司建网站最好鞍山市住房和城乡建设网站
  • 淘宝客网站制作视频教程英雄联盟视频网站源码
  • 免费资料网站网址下载做外卖那些网站好
  • 做板子焊接的网站的公司名字确定建设电子商务网站目的
  • 招聘网站做一下要多少钱加盟型网站制作
  • ps扩展插件网站互联网网站建设价格
  • 织梦的网站关键词商标设计怎么收费
  • 电脑做视频的网站比较好手机编程工具
  • 6做网站网站建设初步认识的实训体会
  • 兰州装修公司哪家口碑好seo排名的方法
  • html手机网站如何制作网站建设和运维单位责任