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

电子商务网站 方案自动化培训网站建设

电子商务网站 方案,自动化培训网站建设,开发语言有哪些,网店美工毕业设计论文支持自定义布局#xff1a;可以灵活地显示自定义样式的 Toast。 线程安全#xff1a;确保在主线程中显示 Toast#xff0c;避免崩溃。 避免内存泄漏#xff1a;使用 ApplicationContext 和取消机制#xff0c;防止内存泄漏问题。 工具类#xff1a;作为一个通用的工具…支持自定义布局可以灵活地显示自定义样式的 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为textTextView 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为textTextView 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); // 当前是子线程切换到主线程运行}} }使用示例 显示自定义布局的 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:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:backgrounddrawable/toast_backgroundandroid:padding16dpandroid:orientationhorizontalImageViewandroid:idid/iconandroid:layout_width24dpandroid:layout_height24dpandroid:srcdrawable/ic_toast_iconandroid:layout_marginEnd8dp/TextViewandroid:idid/textandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textColorandroid:color/whiteandroid:textSize16sp/ /LinearLayout
http://www.hkea.cn/news/14350104/

相关文章:

  • 怎么写网站海外广告投放是干嘛的
  • 中企动力做网站一次性付款永嘉网站制作哪家好
  • wordpress手机管理沈阳免费seo关键词优化排名
  • 优惠券精选网站怎么做网站后台更新文章 前台不显示
  • joomla 2.5:你的网站建设_使用与管理 pdfmvc3网站上传到空间
  • 文山知名网站建设联系电话装修设计装饰公司
  • 新手学做网站从哪里开始做网站需要商标注册吗
  • 广州市官网网站建设哪家好网站开发实训报告参考文献
  • 做网站公司怎样全国十大外贸平台
  • 湘潭网站建设口碑好磐石网络泉州关键词优化
  • 怎么做网站最便宜wap入口
  • 淄博亿泰网站建设推广建设外贸网站注意什么
  • 石家庄网络公司排名企业网站优化面向什么工作
  • 全县网站建设管理工作会议召开美发培训职业学校
  • 买外贸服装去哪个网站河北网站搜索排名优化方案
  • 少主网络建站拼团网站建设
  • 网站正能量大全小制作简单易学
  • 电商类网站开发费用怎么修改网站的源代码
  • 自适应网站推广松江网站建设品划网络
  • 深圳微信网站开发公司网站建设 总体目标
  • 南阳网站运营网站建设评价
  • 主机做网站服务器怎么设置网站上怎么做返回主页链接
  • 免费企业网站空间昆明网站建设方案优化
  • 如何做网络推广赚钱深圳网站开发搜行者seo
  • 云南最便宜的网站建设全网推广成功再收费
  • 织梦网站漏洞伊宁市建设局网站
  • 湖南省建设厅网站购物网站建设市场调查论文
  • 做五金找订单查什么网站企业大全企业名录
  • 南京律师网站建设描述网站开发的流程
  • xxx网站建设策划书范文建立网站 英语怎么说