Android 自定義縮短Toast顯示時(shí)間的實(shí)例代碼
我這個(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)文章
Android 在程序運(yùn)行時(shí)申請(qǐng)權(quán)限的實(shí)例講解
下面小編就為大家分享一篇Android 在程序運(yùn)行時(shí)申請(qǐng)權(quán)限的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
Android style的繼承方式 點(diǎn)(.)和parent詳解及實(shí)例
這篇文章主要介紹了Android style的繼承方式 點(diǎn)(.)和parent詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02
Android開(kāi)發(fā)框架之自定義ZXing二維碼掃描界面并解決取景框拉伸問(wèn)題
這篇文章主要介紹了Android開(kāi)發(fā)框架之自定義ZXing二維碼掃描界面并解決取景框拉伸問(wèn)題的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
簡(jiǎn)單實(shí)現(xiàn)Android鬧鐘程序 附源碼
這篇文章主要幫助大家簡(jiǎn)單實(shí)現(xiàn)Android鬧鐘程序,附源碼下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
詳解Android中Intent傳遞對(duì)象給Activity的方法
這篇文章主要介紹了Android中Intent傳遞對(duì)象給Activity的方法,文章中對(duì)Activity的生命周期等知識(shí)先作了簡(jiǎn)要的介紹,需要的朋友可以參考下2016-04-04
Kotlin學(xué)習(xí)教程之函數(shù)的默認(rèn)參數(shù)
這篇文章主要給大家介紹了關(guān)于Kotlin學(xué)習(xí)教程之函數(shù)的默認(rèn)參數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Android 自定義LayoutManager實(shí)現(xiàn)花式表格
這篇文章主要介紹了Android 自定義LayoutManager實(shí)現(xiàn)花式表格,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Android實(shí)現(xiàn)擴(kuò)大View點(diǎn)擊區(qū)域的三種方式
在 Android 應(yīng)用開(kāi)發(fā)中,有時(shí)候需要擴(kuò)大 View 的點(diǎn)擊區(qū)域以提高用戶交互的便利性,尤其是當(dāng)視圖元素較小或用戶界面密集時(shí),以下提供幾種擴(kuò)大點(diǎn)擊區(qū)域的思路,感興趣的小伙伴跟著小編一起來(lái)看看吧2024-08-08

