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

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

Android超實用的Toast提示框優(yōu)化分享

2019-12-12 05:23:50
字體:
供稿:網(wǎng)友

前言

相信每位Android開發(fā)者都用過Toast,都知道是彈出消息的。類似于js里面的alert,C#里面的MesageBox。當然android里面也有dialogdialog是有焦點的,可與用戶交互。而toast是沒有焦點的,時間到了自動消失,不能回應用戶的交互,下面就跟大家分享下Android中Toast提示框的優(yōu)化方法。

先看下源碼:

public class Toast {   public static final int LENGTH_SHORT = 0;   public static final int LENGTH_LONG = 1;     /**   * 構(gòu)造一個空的toast。你必須在調(diào)動show()之前,線調(diào)用setView()   * @param context 參數(shù),application或者activity都可以   */  public Toast(Context context) {   ...   //獲取系統(tǒng)內(nèi)置的toast_y_offset常量值   mTN.mY = context.getResources().getDimensionPixelSize(     com.android.internal.R.dimen.toast_y_offset);   mTN.mGravity = context.getResources().getInteger(     com.android.internal.R.integer.config_toastDefaultGravity);  }   /**   * 在指定的時長顯示view視圖   */  public void show() {   //如果mNextView為空,即沒有設置view   if (mNextView == null) {    throw new RuntimeException("setView must have been called");   }    //通過系統(tǒng)服務,獲取通知管理器。看來這是使用系統(tǒng)通知?   INotificationManager service = getService();   String pkg = mContext.getOpPackageName();   TN tn = mTN;   tn.mNextView = mNextView;    try {    service.enqueueToast(pkg, tn, mDuration);   } catch (RemoteException e) {    // Empty   }  }    /**   * 關閉一個正在顯示的toast, 或者取消一個未顯示的toast.   * 通常你不必調(diào)用它,在適當?shù)臅r長后它會自動消失的。   */  public void cancel() {   mTN.hide();    try {    getService().cancelToast(mContext.getPackageName(), mTN);   } catch (RemoteException e) {    // Empty   }  }   /**   * 設置toast顯示的視圖內(nèi)容,不單單是黑色的界面,你可以自己決定顯示什么   * @see #getView   */  public void setView(View view) {   mNextView = view;  }   /**   * 設置時長,只能是下面這兩個常量值,沒什么卵用   * @see #LENGTH_SHORT 2000毫秒   * @see #LENGTH_LONG 3500毫秒   */  public void setDuration(@Duration int duration) {   mDuration = duration;  }    /**   * 設置view的外間距,不用多說.   *   * @param horizontalMargin The horizontal margin, in percentage of the   *  container width, between the container's edges and the   *  notification   * @param verticalMargin The vertical margin, in percentage of the   *  container height, between the container's edges and the   *  notification   */  public void setMargin(float horizontalMargin, float verticalMargin) {   mTN.mHorizontalMargin = horizontalMargin;   mTN.mVerticalMargin = verticalMargin;  }    /**   * 設置notification在屏幕中的方位,大家都知道.上中下左中右什么的都有   * @see android.view.Gravity   * @see #getGravity   */  public void setGravity(int gravity, int xOffset, int yOffset) {   mTN.mGravity = gravity;   mTN.mX = xOffset;   mTN.mY = yOffset;  }     /**   * 構(gòu)造一個只包含一個TextView的標準toast對象   *   * @param context 通常是application或者activity對象   * @param text  用于顯示的文本,可以是formatted text.   * @param duration 顯示時長. LENGTH_SHORT或LENGTH_LONG   *   */  public static Toast makeText(Context context, CharSequence text, @Duration int duration) {   Toast result = new Toast(context);    LayoutInflater inflate = (LayoutInflater)     context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    //包含了一個默認的TextView,這個textview的布局位置在  com.android.internal.R.layout.transient_notification,可以去查看下內(nèi)容   View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);   TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);   tv.setText(text);      result.mNextView = v;   result.mDuration = duration;    return result;  }     /**   * 更新通過makeText()方法創(chuàng)建出來的toast對象顯示的文本內(nèi)容   * @param s 待顯示的新文本內(nèi)容.   */  public void setText(CharSequence s) {   if (mNextView == null) {    throw new RuntimeException("This Toast was not created with Toast.makeText()");   }    /**    * 看來com.android.internal.R.layout.transient_notification布局里面的唯一的    * TextView標簽的id是R.id.message。拿到這個textview,設置新文本內(nèi)容    */   TextView tv = (TextView) mNextView.findViewById(com.android.internal.R.id.message);   if (tv == null) {    throw new RuntimeException("This Toast was not created with Toast.makeText()");   }   tv.setText(s);  }    static private INotificationManager getService() {   if (sService != null) {    return sService;   }   //獲取遠程的通知服務   sService = INotificationManager.Stub.asInterface(ServiceManager.getService("notification"));   return sService;  }    //TN是一個瞬態(tài)通知的子類,里面包含顯示和隱藏兩個任務對象  private static class TN extends ITransientNotification.Stub {   final Runnable mShow = new Runnable() {    @Override    public void run() {     handleShow();    }   };    final Runnable mHide = new Runnable() {    @Override    public void run() {     handleHide();     // Don't do this in handleHide() because it is also invoked by handleShow()     mNextView = null;    }   };     //出現(xiàn)Handler了哦   final Handler mHandler = new Handler();     /**    * 調(diào)度handleShow任務到執(zhí)行線程中    */   @Override   public void show() {    if (localLOGV) Log.v(TAG, "SHOW: " + this);    //handler發(fā)送異步任務了    mHandler.post(mShow);   }     /**    * 同上    */   @Override   public void hide() {    if (localLOGV) Log.v(TAG, "HIDE: " + this);    mHandler.post(mHide);   }    //...  }  } 

通過上面的源碼解讀,了解到有遠程通知,handler異步任務等信息,不多說,自己看。

重點是toast的用法:

1、直接調(diào)用makeText靜態(tài)方法即可,返回的是Toast對象,最后別忘了調(diào)用show方法顯示:

Toast.makeText(context, text, duration).show(); 


Toast toast = Toast.makeText(context, text, duration); pre name="code" class="html"> toast.setView(view); toast.setText(s); toast.setGravity(gravity, xOffset, yOffset); toast.setDuration(duration); toast.show();

2、還可以使用new的方式創(chuàng)建,別忘了setView()方法:

Toast toast = new Toast(); toast.setView(view); toast.setText(s); toast.setGravity(gravity, xOffset, yOffset); toast.setDuration(duration); toast.show(); 

以上這些都不值得一提,很簡單。

在開發(fā)過程中,有這樣的需求:在項目總,我們偷懶,想連串toast出多個變量的值或者其他任務,可在操作手機時直觀可見。問題來了,彈出是無論我們的操作有多快,這些toast內(nèi)容都是一個跟著一個顯示,沒辦法快進。哪怕我們玩完了,退出了app,它還在彈。怎么辦?有沒有辦法讓toast的內(nèi)容與我們的操作同步,快速反應?

public class T {  private static Toast toast;   public static void show(Context context, String msg) {   if (toast == null) {    toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);   } else {    toast.setText(msg);   }   toast.show();  } } 

單例模式,每次創(chuàng)建toast都調(diào)用這個類的show方法,Toast的生命周期從show開始,到自己消失或者cancel為止。如果正在顯示,則修改顯示的內(nèi)容,你持有這個對象的引用,當然可以修改顯示的內(nèi)容了。若每次你都makeText或者new一個toast對象,即每次通過handler發(fā)送異步任務,調(diào)用遠程通知服務顯示通知,當然是排隊等待顯示了。

結(jié)束語

以上就是Android中Toast提示框優(yōu)化的全部內(nèi)容,希望對大家開發(fā)Android能有所幫助,如果有大家有疑問可以留言交流。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 牙克石市| 元氏县| 大庆市| 岳池县| 五河县| 揭东县| 龙游县| 罗平县| 三原县| 仙居县| 高邮市| 上饶市| 南乐县| 即墨市| 沙湾县| 神木县| 武夷山市| 浏阳市| 海城市| 陵川县| 松潘县| 永春县| 鹤庆县| 清远市| 固安县| 宜丰县| 车险| 钟祥市| 绵竹市| 喀喇沁旗| 福建省| 东丽区| 东宁县| 齐齐哈尔市| 怀化市| 潍坊市| 潍坊市| 临沭县| 青铜峡市| 郴州市| 纳雍县|