Android编程实现Toast只显示最后一条的方法
本文实例讲述了Android编程实现Toast只显示最后一条的方法。分享给大家供大家参考,具体如下:
在做Android开发中,时不时的可能会用到Toast,但用Toast的时候,连续使用会存在一个问题,就是一条条显示Toast。而不是直接显示最后一条。因此,根据此需求,现在写了ToastUtil这个类,该类中有三个方法供选择。
ToastUtil.Java
importandroid.content.Context; importandroid.graphics.PixelFormat; importandroid.os.Handler; importandroid.os.Looper; importandroid.os.Message; importandroid.view.Gravity; importandroid.view.WindowManager; importandroid.widget.TextView; importandroid.widget.Toast; publicclassToastUtil{ //方法一 privatestaticHandlermHandler=newHandler(Looper.getMainLooper()); privatestaticToastmToast=null; privatestaticObjectsynObject=newObject(); publicstaticvoidshowToastByThread(Contextcontext,Stringmsg){ showToastByThread(context,msg,Toast.LENGTH_SHORT); } publicstaticvoidshowToastByThread(Contextcontext,intmsg){ showToastByThread(context,context.getText(msg),Toast.LENGTH_SHORT); } publicstaticvoidshowToastByThread(finalContextcontext,finalCharSequencemsg,finalintlength){ newThread(newRunnable(){ @Override publicvoidrun(){ mHandler.post(newRunnable(){ @Override publicvoidrun(){ synchronized(synObject){ if(mToast!=null){ mToast.setText(msg); mToast.setDuration(length); }else{ mToast=Toast.makeText(context,msg,length); } mToast.show(); } } }); } }).start(); } //方法二:注意此方法不能再子线程中使用 privatestaticlongoneTime; privatestaticlongtwoTime; privatestaticStringoldMsg; publicstaticvoidshowToastByTime(Contextcontext,Stringmsg){ if(mToast==null){ mToast=Toast.makeText(context,msg,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; } publicstaticvoidshowToastByTime(Contextcontext,intmsg){ showToastByTime(context,context.getString(msg)); } //方法三 publicstaticTextViewmTextView; publicstaticWindowManagermWindowManager=null; privatestaticHandlermPriHandler=newHandler(){ @Override publicvoidhandleMessage(Messagemsg){ cancelToast(); } }; publicstaticvoidshowToastByWindow(Contextcontext,Stringmsg){ mWindowManager=(WindowManager)context.getSystemService(Context.WINDOW_SERVICE); if(mTextView==null){ mTextView=newTextView(context); } mTextView.setText(msg); mTextView.setTextSize(20); mTextView.setPadding(0,0,0,30); if(mTextView.getParent()==null){ WindowManager.LayoutParamsparams=newWindowManager.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); } } publicstaticvoidcancelToast(){ if(mTextView!=null&&mTextView.getParent()!=null){ mWindowManager.removeView(mTextView); } } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android布局layout技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。