最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android 自定義縮短Toast顯示時(shí)間的實(shí)例代碼

 更新時(shí)間:2020年01月05日 15:02:19   作者:云卷云舒i  
這篇文章主要介紹了Android 自定義縮短Toast顯示時(shí)間,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

我這個(gè)主要是縮短Toast顯示時(shí)間,要延長(zhǎng)時(shí)間的話,可自行更改

廢話不多說(shuō)哈,見(jiàn)代碼

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...");
  }
  /**
   * 自定義時(shí)長(zhǎng)、居中顯示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...");
  }
  /**
   * 自定義計(jì)時(shí)器
   */
  private class TimeCount extends CountDownTimer {
    public TimeCount(long millisInFuture, long countDownInterval) {
      super(millisInFuture, countDownInterval); //millisInFuture總計(jì)時(shí)長(zhǎng),countDownInterval時(shí)間間隔(一般為1000ms)
    }
    @Override
    public void onTick(long millisUntilFinished) {
      Log.e(TAG, ": " + millisUntilFinished / 100 + "后消失" );
    }
    @Override
    public void onFinish() {
      hide();//記數(shù)結(jié)束后調(diào)用取消Toast的顯示
    }
  }
}

使用方式:

ToastUtil toastUtil = new ToastUtil(MainActivity.this,"保存成功!");//MainActivity.this為
//Context,

toastUtil.show(500);

總結(jié)

以上所述是小編給大家介紹的Android 自定義縮短Toast顯示時(shí)間的實(shí)例代碼,希望對(duì)大家有所幫助!

相關(guān)文章

最新評(píng)論

图们市| 东海县| 闽清县| 沾益县| 澄城县| 文昌市| 冷水江市| 巍山| 江阴市| 平山县| 中江县| 自治县| 乐山市| 巢湖市| 额敏县| 丰镇市| 台北县| 韶山市| 综艺| 澎湖县| 原阳县| 赣榆县| 陕西省| 北碚区| 马边| 工布江达县| 光泽县| 施甸县| 临猗县| 井冈山市| 读书| 龙岩市| 中江县| 壶关县| 绥棱县| 淮阳县| 锡林郭勒盟| 加查县| 长治县| 本溪市| 上犹县|