欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android编程实现Toast只显示最后一条的方法

程序员文章站 2023-12-14 22:12:34
本文实例讲述了android编程实现toast只显示最后一条的方法。分享给大家供大家参考,具体如下: 在做android开发中,时不时的可能会用到toast,但用toas...

本文实例讲述了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, msg, 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, 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;
  }
  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, 0, 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程序设计有所帮助。

上一篇:

下一篇: