本文实例讲述了AndroID编程实现Toast只显示最后一条的方法。分享给大家供大家参考,具体如下:
在做AndroID开发中,时不时的可能会用到Toast,但用Toast的时候,连续使用会存在一个问题,就是一条条显示Toast。而不是直接显示最后一条。因此,根据此需求,现在写了ToastUtil这个类,该类中有三个方法供选择。
ToastUtil.Java
import androID.content.Context;import androID.graphics.PixelFormat;import androID.os.Handler;import androID.os.Looper;import androID.os.Message;import androID.vIEw.Gravity;import androID.vIEw.WindowManager;import androID.Widget.TextVIEw;import androID.Widget.Toast;public class ToastUtil { //方法一 private static Handler mHandler = new Handler(Looper.getMainLooper()); private static Toast mToast = null; private static Object synObject = new Object(); public static voID showToastByThread(Context context,String msg){ showToastByThread(context,msg,Toast.LENGTH_SHORT); } public static voID showToastByThread(Context context,int msg){ showToastByThread(context,context.getText(msg),Toast.LENGTH_SHORT); } public static voID showToastByThread(final Context context,final CharSequence msg,final int length){ new Thread(new Runnable() { @OverrIDe public voID run() { mHandler.post(new Runnable() { @OverrIDe public voID run() { synchronized (synObject) { if (mToast != null){ mToast.setText(msg); mToast.setDuration(length); }else{ mToast = Toast.makeText(context,length); } mToast.show(); } } }); } }).start(); } //方法二:注意此方法不能再子线程中使用 private static long oneTime; private static long twoTime; private static String oldMsg; public static voID showToastByTime(Context context,String msg){ if (mToast == null) { mToast = Toast.makeText(context,Toast.LENGTH_SHORT); mToast.show(); oneTime = System.currentTimeMillis(); } else { twoTime = System.currentTimeMillis(); if (msg.equals(oldMsg)){ if (twoTime-oneTime > Toast.LENGTH_SHORT){ mToast.show(); } } else { oldMsg = msg; mToast.setText(msg); mToast.show(); } } oneTime = twoTime; } public static voID showToastByTime(Context context,int msg){ showToastByTime(context,context.getString(msg)); } //方法三 public static TextVIEw mTextVIEw; public static WindowManager mWindowManager = null; private static Handler mPriHandler = new Handler(){ @OverrIDe public voID handleMessage(Message msg) { cancelToast(); } }; public static voID showToastByWindow(Context context,String msg){ mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); if (mTextVIEw == null){ mTextVIEw = new TextVIEw(context); } mTextVIEw.setText(msg); mTextVIEw.setTextSize(20); mTextVIEw.setpadding(0,30); if (mTextVIEw.getParent() == null){ WindowManager.LayoutParams params = new WindowManager.LayoutParams(); params.gravity= Gravity.BottOM; params.Alpha = 0.65f; params.x = 0; params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.wIDth = WindowManager.LayoutParams.WRAP_CONTENT; params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_touchABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; params.format = PixelFormat.TRANSLUCENT; params.windowAnimations = 0; mWindowManager.addVIEw(mTextVIEw,params); mPriHandler.sendEmptyMessageDelayed(101,1000); } else { mTextVIEw.setText(msg); mPriHandler.removeMessages(101); mPriHandler.sendEmptyMessageDelayed(101,1000); } } public static voID cancelToast(){ if (mTextVIEw != null && mTextVIEw.getParent() != null){ mWindowManager.removeVIEw(mTextVIEw); } }}
更多关于AndroID相关内容感兴趣的读者可查看本站专题:《Android布局layout技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》及《Android控件用法总结》
希望本文所述对大家AndroID程序设计有所帮助。
总结以上是内存溢出为你收集整理的Android编程实现Toast只显示最后一条的方法全部内容,希望文章能够帮你解决Android编程实现Toast只显示最后一条的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)