自定义Toast

来源:互联网 发布:淘宝店卖假货会怎么样 编辑:程序博客网 时间:2024/06/03 07:39
本篇博客主要解决Toast重复显示和自定义显示时长,当然解决问题的方法很多,我在这主要讲的是通过Handler控制Toast的重复显示,欢迎大家批评与指正。直接上代码:
@Override    public void show(Context ctx, CharSequence content, int time) {        time = time < 1000?2000:time;        mHandler.removeCallbacks(runnable);//remove上一次的请求        mHandler.postDelayed(runnable,time);        showToast(ctx,content,time);    }
private void showToast(Context ctx, CharSequence content, int time){        Context applicationContext = ctx.getApplicationContext();//获取全局Context        if (!TextUtils.isEmpty(content)){            if (null == toast){                toast = Toast.makeText(applicationContext,content,Toast.LENGTH_SHORT);            }            toast.show();        }    } private Runnable runnable = new Runnable() {        @Override        public void run() {            JToast.toast.cancel();        }    };

就这样,Toast的重复显示和自定义显示时长就搞定了。

0 0