我这个主要是缩短Toast显示时间,要延长时间的话,可自行更改
废话不多说哈,见代码
import androID.content.Context;import androID.os.CountDownTimer;import androID.util.Log;import androID.Widget.Toast;public class ToastUtil { private String TAG = "ToastUtil"; private Toast mToast; private TimeCount timeCount; private String message; private boolean canceled = true; public ToastUtil(Context context,String msg) { message = msg; Log.i("ToastUtil","Toast start..."); if (mToast == null) { mToast =Toast.makeText(context,message,Toast.LENGTH_SHORT); Log.i("ToastUtil","Toast create..."); } } /** * 自定义居中显示toast */ public voID show() { mToast.show(); Log.i("ToastUtil","Toast show..."); } /** * 自定义时长、居中显示toast * @param duration */ public voID show(int duration) { timeCount = new TimeCount(duration,100); Log.i("ToastUtil","Toast show..."); if (canceled) { timeCount.start(); show(); canceled = false; } } /** * 隐藏toast */ private voID hIDe() { if (mToast != null) { mToast.cancel(); } canceled = true; Log.i("ToastUtil","Toast that customed duration hIDe..."); } /** * 自定义计时器 */ private class TimeCount extends CountDownTimer { public TimeCount(long millisInFuture,long countDownInterval) { super(millisInFuture,countDownInterval); //millisInFuture总计时长,countDownInterval时间间隔(一般为1000ms) } @OverrIDe public voID onTick(long millisUntilFinished) { Log.e(TAG,": " + millisUntilFinished / 100 + "后消失" ); } @OverrIDe public voID onFinish() { hIDe();//记数结束后调用取消Toast的显示 } }}
使用方式:
ToastUtil toastUtil = new ToastUtil(MainActivity.this,"保存成功!");//MainActivity.this为//Context,toastUtil.show(500);
总结
以上所述是小编给大家介绍的AndroID 自定义缩短Toast显示时间的实例代码,希望对大家有所帮助!
总结以上是内存溢出为你收集整理的Android 自定义缩短Toast显示时间的实例代码全部内容,希望文章能够帮你解决Android 自定义缩短Toast显示时间的实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)