Toast常用问题处理办法-时间叠加-自定义
来源:互联网 发布:为什么四川美女多 知乎 编辑:程序博客网 时间:2024/06/05 04:06
Toast常用问题处理办法-时间叠加-自定义
Toast 概述
Toast 作用
与AlertDialog.Builder对话框不同,toast是不需要与用户交互的提示框。
Toast 使用
Toast 基本使用
Toast.makeText(上下文,显示文本, 显示时长).show();
第一个上下文就是Context,
第二个就是显示的内容,可以选择用字符串资源文件ID也可以直接编辑内容
第三个是显示的时长,android给了我们时长的常量Toast.LENGTH_SHORT(2秒) Toast.LENGTH_LONG(3.5秒)
Toast 基本使用 之 多次显示,时长叠加问题处理办法(一)
如果在使用Toast中,不需要自定义Toast的话,处理时间叠加的问题,此方法最佳,并且此方法在点击Toast时会显示最后一次toast的内容,实现内容覆盖
public class ToastUtils {
private static Toast toast = null;
/**
* 判断toast是否存在,如果存在则更新text,达到避免出现时间叠加的问题
* @param context 上下文
* @param text 显示的内容
* @param duration 显示时长,默认值为Toast.LENGTH_SHORT (2秒)或Toast.LENGTH_LONG(3.5秒)
*/
public static void toastShow(Context context, String text, int duration) {
if (toast == null) {
toast = Toast.makeText(context, text, duration);
} else {
toast.setText(text);
}
toast.show();
}
/**
* 判断toast是否存在,如果存在则更新text,达到避免出现时间叠加的问题
* @param context 上下文
* @param resId 字符串资源文件ID
* @param duration 显示时长,默认值为Toast.LENGTH_SHORT (2秒)或Toast.LENGTH_LONG(3.5秒)
*/
public static void toastShow(Context context, int resId, int duration)
throws Resources.NotFoundException {
if (toast == null) {
toast = Toast.makeText(context, context.getResources().getText(resId), duration);
} else {
toast.setText(context.getResources().getText(resId));
}
toast.show();
}
}
Toast 基本使用 之 多次显示,时长叠加问题处理办法(二)
此办法相比上一个处理办法,实现方式不同,此方法利用了同步锁,同样实现了叠加问题。调用方法:ToastUtil.getInstance().showToast(上下文,文本);
public class ToastUtil {
private static Toast toast = null;
private static ToastUtil toastUtil = null;
public ToastUtil(){}
public synchronized static ToastUtil getInstance(){
if(null == toastUtil){
toastUtil = new ToastUtil();
}
return toastUtil;
}
public void showToast(Context context, String string){
if(toast != null){
toast.cancel();
}
toast = Toast.makeText(context, string,Toast.LENGTH_SHORT);
toast.show();
}
}
Toast 自定义
此方法,可以保证自定义的前提下,处理重复操作时间叠加的问题,但不是最好方法,因为如果点击A显示AToast,点击B显示BToast,那么点击过程AAB,将只会展示AToast的内容,BToast内容将不会展示,即会被跳过。
public class ToastUtil {
private static Toast mToast;
private static Handler mHandler = new Handler();
private static Runnable r = new Runnable() {
public void run() {
mToast.cancel();
mToast = null;// toast隐藏后,将其置为null
}
};
public static void showShortToast(Context context, String message) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_toast, null);// 自定义布局
TextView text = (TextView) view.findViewById(R.id.toast_message);// 显示的提示文字
text.setText(message);
mHandler.removeCallbacks(r);
if (mToast == null) {// 只有mToast==null时才重新创建,否则只需更改提示文字
mToast = new Toast(context);
mToast.setDuration(Toast.LENGTH_SHORT);
mToast.setGravity(Gravity.BOTTOM, 0, 150);
mToast.setView(view);
}
mHandler.postDelayed(r, 1000);// 延迟1秒隐藏toast
mToast.show();
}
}
- Toast常用问题处理办法-时间叠加-自定义
- [读书笔记] Android Toast 显示时间叠加问题的探讨
- Android Toast显示叠加的问题
- Toast自定义时间
- Toast类避免显示时间叠加的方法
- Toast显示时间的自定义
- android toast自定义显示时间
- android 自定义toast停留时间
- Android toast弹出时间自定义
- 自定义Toast悬浮停靠问题
- 自定义toast 显示最新toast不累加时间
- Toast重复显示问题处理
- 自定义Toast,防止层叠显示问题,和自定义Toast样式
- Android中Toast显示时间的自定义
- Android进阶篇-Toast自定义显示时间
- Android中Toast显示时间的自定义
- Android中Toast显示时间的自定义
- android 自定义Toast,可设定显示时间
- spring cloud 的启动Eureka client报错--Cannot execute request on any known server
- eclipse加载别人的工程时提示the import java.util cannot be resolve时解决方式
- 填充因子
- POJ 1222 EXTENDED LIGHTS OUT
- css基础之---无情的定位position
- Toast常用问题处理办法-时间叠加-自定义
- Android图片加载框架Glide用法
- Android Studio入门到精通
- hdu 3746 Cyclic Nacklace(KMP中next数组的运用)
- CentOS6.8 安装SVN服务器
- Dobbo的继任者?试用微博RPC框架Motan
- Android中的Gilde
- Android Transtion
- 进程的虚地址