Android 自定义 Toast 显示时间实现代码:package com.wm.realname.util;import Android.content.Context;import android.os.Handler;import
Android 自定义 Toast 显示时间
实现代码:
package com.wm.realname.util;import Android.content.Context;import android.os.Handler;import android.view.View;import android.widget.Toast;public class ToastUtil { private Context mContext = null; private Toast mToast = null; private Handler mHandler = null; private int duration = 0; private int currDuration = 0; private final int DEFAULT = 2000; private Runnable mToastThread = new Runnable() { public void run() { mToast.show(); mHandler.postDelayed(mToastThread, DEFAULT); // 每隔2秒显示一次 if (duration != 0) { if (currDuration <= duration) { currDuration += DEFAULT; } else { cancel(); } } } } public ToastUtil(Context context) { mContext = context; currDuration = DEFAULT; mHandler = new Handler(mContext.getMainLooper()); mToast = Toast.makeText(mContext, "", Toast.LENGTH_LONG); } public void setText(String text) { mToast.setText(text); } public void show(int duration) { this.duration = duration; mHandler.post(mToastThread); } public void setGravity(int gravity, int xOffset, int yOffset) { mToast.setGravity(gravity, xOffset, yOffset); } public void setDuration(int duration) { mToast.setDuration(duration); } public void setView(View view) { mToast.setView(view); } public void cancel( ) { mHandler.removeCallbacks(mToastThread);// 先把显示线程删除 mToast.cancel();// 把最后一个线程的显示效果cancel掉,就一了百了了 currDuration = DEFAULT; }}
--结束END--
本文标题: Android 自定义 Toast 显示时间
本文链接: https://lsjlt.com/news/222911.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0