国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android Toast自定義顯示時(shí)間

2019-12-12 00:33:19
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Toast是Android中使用頻率較高的彈窗提示手段,使用起來(lái)簡(jiǎn)單、方便。常規(guī)使用方法這里不做說(shuō)明,繼前一篇博客《Android中Toast全屏顯示》 ,其中拋磚引玉的給出一個(gè)簡(jiǎn)單的實(shí)現(xiàn)Toast全屏顯示的方法后,發(fā)現(xiàn)無(wú)法控制Toast的顯示時(shí)長(zhǎng)。雖然Toast中有setDuration(int duration)接口,但是跟蹤代碼發(fā)現(xiàn),設(shè)置的時(shí)間沒(méi)起作用,只有系統(tǒng)默認(rèn)的兩個(gè)時(shí)間LENGTH_DURATION = 3500毫秒,SHORT_DURATION = 2000毫秒。也就是說(shuō),無(wú)論我們?cè)O(shè)置多長(zhǎng)時(shí)間,最終影響Toast彈窗時(shí)間的只有Toast.LENGTH_LONG和Toast.LENGTH_SHORT兩個(gè)參數(shù)。

目前解決該問(wèn)題的方法主要有兩個(gè):

1、利用反射原理,通過(guò)控制Toast的show()和hide()接口來(lái)控制顯示時(shí)間,可參見(jiàn)博客《利用反射機(jī)制控制Toast的顯示時(shí)間》。不過(guò)該方法只對(duì)Android4.0以下的系統(tǒng)有效,通過(guò)模擬器實(shí)測(cè),也是如此。當(dāng)前系統(tǒng)基本都在Android4.0以上,該方法過(guò)于老舊。

2、利用WindowManager的addView()方法動(dòng)態(tài)刷屏,可看見(jiàn)博客《Android自定義Toast,可設(shè)定顯示時(shí)間》 。該方法被很多軟件用來(lái)顯示浮動(dòng)窗口和圖片的動(dòng)態(tài)懸浮效果,如360手機(jī)軟件和一些手游軟件。在Android4.0上是一種不錯(cuò)的選擇。當(dāng)然,對(duì)于遇到系統(tǒng)默認(rèn)把懸浮窗口功能關(guān)閉的手機(jī),這招可能就不靈了。

通過(guò)分析Toast的顯示原理和彈窗控制邏輯,本人借助Handler和Runnable機(jī)制,也成功實(shí)現(xiàn)了對(duì)Toast顯示任意自定義時(shí)長(zhǎng)。代碼是在Toast全屏顯示的基礎(chǔ)上修改而來(lái),貼出如下:

package com.dls.nltest; import android.content.Context;import android.os.Handler;import android.util.DisplayMetrics;import android.util.Log;import android.view.Gravity;import android.view.WindowManager;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;import android.widget.LinearLayout.LayoutParams; public class GenericToast{ private static final String TAG = "GenericToast";  private static final int TOAST_TEXTSIZE = 20;  /** {@link Toast#LENGTH_SHORT} default time is 3500ms */ private static final int LENGTH_SHORT_TIME = 2000;  private static Context mContext = null;  private static Toast mToast = null; private static TextView mTextView = null; private static int mDuration = 0; private static CharSequence mText = null;  private Handler mHandler = new Handler();  private GenericToast(Context context) { mContext = context; }  public static GenericToast makeText(Context context, CharSequence text, int duration){ GenericToast instance = new GenericToast(context); mContext = context; mDuration = duration; mText = text; return instance; }  private static void getToast(Context context, CharSequence text){ mToast = Toast.makeText(context, null, Toast.LENGTH_LONG); mToast.setGravity(Gravity.CENTER, 0, 0); LinearLayout toastView = (LinearLayout)mToast.getView();  // Get the screen size with unit pixels. WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics);  mTextView = new TextView(context); LayoutParams vlp = new LayoutParams(outMetrics.widthPixels,        outMetrics.heightPixels);  vlp.setMargins(0, 0, 0, 0); mTextView.setLayoutParams(vlp); mTextView.setTextSize(TOAST_TEXTSIZE); mTextView.setText(text); mTextView.setGravity(Gravity.CENTER); toastView.addView(mTextView); }  /** * Before call this method, you should call {@link makeText}. * * @return Toast display duration. */ public int getDuration(){ return mDuration; }  public void show(){ Log.d(TAG, "Show custom toast"); mHandler.post(showRunnable); }  public void hide(){ Log.d(TAG, "Hide custom toast"); mDuration = 0; if(mToast != null){ mToast.cancel(); } }  private Runnable showRunnable = new Runnable(){ @Override public void run() { if(mToast != null){ mTextView.setText(mText); }else{ getToast(mContext, mText); } if(mDuration != 0){ mToast.show(); }else{ Log.d(TAG, "Hide custom toast in runnable"); hide(); return; }  if(mDuration > LENGTH_SHORT_TIME){ mHandler.postDelayed(showRunnable, LENGTH_SHORT_TIME); mDuration -= LENGTH_SHORT_TIME; }else{ mHandler.postDelayed(showRunnable, mDuration); mDuration = 0; } } };}

Toast彈窗10s,測(cè)試代碼如下:

GenericToast mGToast = GenericToast.makeText(this, "I am generic toast", 10 * 1000);mGToast.show();

如果需要終止彈窗,只要在需要的地方調(diào)用hide()即可。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 吉木萨尔县| 阿鲁科尔沁旗| 皮山县| 雷山县| 大英县| 平舆县| 双牌县| 光山县| 上饶县| 萍乡市| 尚志市| 新闻| 壤塘县| 大冶市| 西平县| 溧阳市| 科尔| 鲜城| 梁平县| 汾西县| 祁阳县| 花莲市| 靖安县| 镇宁| 安泽县| 清徐县| 全州县| 毕节市| 类乌齐县| 翁牛特旗| 白玉县| 西平县| 敦化市| 吉安市| 古田县| 民乐县| 弥勒县| 扎赉特旗| 南雄市| 涞水县| 锡林浩特市|