今天就跟大家聊聊有關(guān)Android 中如何自定義縮短Toast顯示時(shí)間,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),枝江企業(yè)網(wǎng)站建設(shè),枝江品牌網(wǎng)站建設(shè),網(wǎng)站定制,枝江網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,枝江網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
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);
看完上述內(nèi)容,你們對(duì)Android 中如何自定義縮短Toast顯示時(shí)間有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
分享題目:Android中如何自定義縮短Toast顯示時(shí)間
地址分享:http://chinadenli.net/article30/gsgjso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、云服務(wù)器、品牌網(wǎng)站設(shè)計(jì)、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站策劃、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)