Android 自定义缩短Toast显示时间的实例代码
程序员文章站
2022-06-24 16:21:53
我这个主要是缩短toast显示时间,要延长时间的话,可自行更改
废话不多说哈,见代码
import android.content.context;
import and...
我这个主要是缩短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 scrollview如何监听滑动状态
下一篇: Android倒计时功能的实现代码