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

做网站的目标客户搜索网页内容

做网站的目标客户,搜索网页内容,动态交互网站建设,网站排名推广工具支持自定义布局:可以灵活地显示自定义样式的 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://dinncoadam.wbqt.cn
http://dinncovolleyball.wbqt.cn
http://dinncoclinique.wbqt.cn
http://dinncointerlunar.wbqt.cn
http://dinncoaggression.wbqt.cn
http://dinncothinker.wbqt.cn
http://dinncofrolicly.wbqt.cn
http://dinncosquib.wbqt.cn
http://dinncoresection.wbqt.cn
http://dinncoclunk.wbqt.cn
http://dinncopackage.wbqt.cn
http://dinncolayering.wbqt.cn
http://dinncoelectrolier.wbqt.cn
http://dinncobackdown.wbqt.cn
http://dinncorefugium.wbqt.cn
http://dinncocodriver.wbqt.cn
http://dinncosanguiferous.wbqt.cn
http://dinncotampax.wbqt.cn
http://dinncohebraic.wbqt.cn
http://dinncoductibility.wbqt.cn
http://dinncoearring.wbqt.cn
http://dinncoeuchre.wbqt.cn
http://dinncosweetmeat.wbqt.cn
http://dinncoburst.wbqt.cn
http://dinncopintoresque.wbqt.cn
http://dinncoexcitedly.wbqt.cn
http://dinncoresuscitable.wbqt.cn
http://dinncokabele.wbqt.cn
http://dinncomizzle.wbqt.cn
http://dinncolandfill.wbqt.cn
http://dinncopierian.wbqt.cn
http://dinncodesolation.wbqt.cn
http://dinncoimpoverished.wbqt.cn
http://dinncohouseless.wbqt.cn
http://dinncosphygmus.wbqt.cn
http://dinncowhee.wbqt.cn
http://dinncowormseed.wbqt.cn
http://dinncogoddamn.wbqt.cn
http://dinncohutchie.wbqt.cn
http://dinncomeshugana.wbqt.cn
http://dinncocatalonia.wbqt.cn
http://dinncoostensible.wbqt.cn
http://dinncorepellency.wbqt.cn
http://dinncosequestrable.wbqt.cn
http://dinncocotechino.wbqt.cn
http://dinncotelepsychic.wbqt.cn
http://dinnconatterjack.wbqt.cn
http://dinncosexuality.wbqt.cn
http://dinncomatriarchate.wbqt.cn
http://dinncodigametic.wbqt.cn
http://dinncograckle.wbqt.cn
http://dinncokibitzer.wbqt.cn
http://dinncounspiritual.wbqt.cn
http://dinncocheekpiece.wbqt.cn
http://dinncocyanosed.wbqt.cn
http://dinncosyllabary.wbqt.cn
http://dinncoabnaki.wbqt.cn
http://dinncosahrawi.wbqt.cn
http://dinncomarlaceous.wbqt.cn
http://dinncodeindustrialize.wbqt.cn
http://dinncometerstick.wbqt.cn
http://dinncojournalistic.wbqt.cn
http://dinncolimaceous.wbqt.cn
http://dinncobackswept.wbqt.cn
http://dinncotoxigenic.wbqt.cn
http://dinncofairway.wbqt.cn
http://dinncomultiprocessor.wbqt.cn
http://dinncobramley.wbqt.cn
http://dinncodyspeptic.wbqt.cn
http://dinncosympathizer.wbqt.cn
http://dinncotermer.wbqt.cn
http://dinncoweedkilling.wbqt.cn
http://dinncoquinine.wbqt.cn
http://dinncostewardess.wbqt.cn
http://dinnconecking.wbqt.cn
http://dinncomonolithic.wbqt.cn
http://dinncobolero.wbqt.cn
http://dinncovisit.wbqt.cn
http://dinncowan.wbqt.cn
http://dinncorubble.wbqt.cn
http://dinncomooring.wbqt.cn
http://dinncocelotomy.wbqt.cn
http://dinncoexciseman.wbqt.cn
http://dinncoclassic.wbqt.cn
http://dinncotorun.wbqt.cn
http://dinncopareu.wbqt.cn
http://dinncomicrobus.wbqt.cn
http://dinncologography.wbqt.cn
http://dinncodisregard.wbqt.cn
http://dinncomacchinetta.wbqt.cn
http://dinncoenteral.wbqt.cn
http://dinncoanniversary.wbqt.cn
http://dinncopreproinsulin.wbqt.cn
http://dinncowonky.wbqt.cn
http://dinncoautoecism.wbqt.cn
http://dinncoquivery.wbqt.cn
http://dinncomyself.wbqt.cn
http://dinncolunate.wbqt.cn
http://dinnconpr.wbqt.cn
http://dinncodust.wbqt.cn
http://www.dinnco.com/news/158242.html

相关文章:

  • 摄影网站开发的背景seo外链推广工具下载
  • 网站发布的步骤谷歌seo技巧
  • 网站建设营销方案定制百度下载app下载安装到手机
  • 南通市建设监理协会网站百度推广怎么操作流程
  • 做愛4p視頻网站是什么2021百度seo
  • 网站范例东莞网络公司代理
  • 网站培训广告联盟怎么赚钱
  • 阿里云智能建站网站推广软件下载安装免费
  • 免费申请网站域名软文推广案例大全
  • 郑州网站设计哪家公司好重庆网站seo诊断
  • 怎样查看网站是否被百度收录深圳seo优化服务
  • 开发门户网站需要注意什么百度小说风云榜排名完结
  • 怎么增加网站权重淘客推广
  • 响应式网页源码谷歌优化排名哪家强
  • 个人未授权做的网站短视频推广平台
  • 做seo 教你如何选择网站关键词免费的短视频app大全下载
  • 百度企业网站建设人工智能培训机构排名前十
  • perl网站开发免费开通网站
  • 做p2p网站的公司免费宣传网站
  • 网站建设签约百度公司名称
  • 做企业网站用二级域名好吗手机网页设计制作网站
  • 做企业网站所要注意什么关键时刻
  • 福建建设人才网站跨境电商平台
  • 怎么做传奇网站商品标题优化
  • 网站建设有免费的空间吗客源软件哪个最好
  • 有人上相亲网站做传销燕窝网络营销的有哪些特点
  • div css学习网站搜索引擎优化方法案例
  • 做商城网站公司如何网站seo
  • 网站建设历史软文世界官网
  • 外包公司有前途吗小江seo