当前位置: 移动技术网 > IT编程>移动开发>Android > Android 自定义缩短Toast显示时间的实例代码

Android 自定义缩短Toast显示时间的实例代码

2020年03月09日  | 移动技术网IT编程  | 我要评论

郭德纲2010,类风湿关节炎吃什么,亚洲有码区

我这个主要是缩短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显示时间的实例代码,希望对大家有所帮助!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网