Toast常用问题处理办法-时间叠加-自定义
2017-01-18 13:54
405 查看
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();
}
}
相关文章推荐
- [读书笔记] Android Toast 显示时间叠加问题的探讨
- 处理MySQL中0时时间数据问题的办法
- SVN常用问题处理办法(待补充)
- MySQL 5.0.16 乱码问题处理办法
- jsp 中关于日期时间处理的几个问题
- 利用HttpHandler处理自定义控件中需要引用大量js文件问题
- 利用HttpHandler处理自定义控件中需要引用大量js文件问题
- 在JAVA开发中的中文处理问题及解决办法
- Windows没有北京时间(GMT+8时区)的问题,及QQ聊天栏中显示的时间错误的问题解决办法
- 图像实时处理常用时间间隔计算方法(微秒级)
- SQL常用时间处理
- PB9自定义纸张问题的解决办法
- Windows2000域用户时间同步问题的解决办法
- MySQL 5.0.16 乱码问题处理办法
- MySQL 5.0.16 乱码问题处理办法
- Trac - 常见问题 - 导出的csv格式文件在Excel中处理unix时间戳
- 常用的时间日期处理类
- struts时间格式转换问题处理方案
- 利用HttpHandler处理自定义控件中需要引用大量js文件问题
- 利用timestamp实现数据库处理同一记录的时间差异问题