您的位置:首页 > 其它

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();

    }

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: