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

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

 更新時間:2016年09月05日 15:38:44   投稿:daisy  
Toast是Android中用來顯示顯示信息的一種機制,和Dialog不一樣的是,Toast是沒有焦點的,而且Toast顯示的時間有限,過一定的時間就會自動消失。那么這篇文章跟大家分享下Android中Toast的優(yōu)化,對大家日常開發(fā)還是很實用,下面來一起看看吧。

前言

相信每位Android開發(fā)者都用過Toast,都知道是彈出消息的。類似于js里面的alert,C#里面的MesageBox。當然android里面也有dialog,dialog是有焦點的,可與用戶交互。而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為空,即沒有設(shè)置view 
  if (mNextView == null) { 
   throw new RuntimeException("setView must have been called"); 
  } 
 
  //通過系統(tǒng)服務(wù),獲取通知管理器。看來這是使用系統(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 
  } 
 } 
 
 
 /** 
  * 關(guān)閉一個正在顯示的toast, 或者取消一個未顯示的toast. 
  * 通常你不必調(diào)用它,在適當?shù)臅r長后它會自動消失的。 
  */ 
 public void cancel() { 
  mTN.hide(); 
 
  try { 
   getService().cancelToast(mContext.getPackageName(), mTN); 
  } catch (RemoteException e) { 
   // Empty 
  } 
 } 
 
 /** 
  * 設(shè)置toast顯示的視圖內(nèi)容,不單單是黑色的界面,你可以自己決定顯示什么 
  * @see #getView 
  */ 
 public void setView(View view) { 
  mNextView = view; 
 } 
 
 /** 
  * 設(shè)置時長,只能是下面這兩個常量值,沒什么卵用 
  * @see #LENGTH_SHORT 2000毫秒 
  * @see #LENGTH_LONG 3500毫秒 
  */ 
 public void setDuration(@Duration int duration) { 
  mDuration = duration; 
 } 
 
 
 /** 
  * 設(shè)置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; 
 } 
 
 
 /** 
  * 設(shè)置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,設(shè)置新文本內(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; 
  } 
  //獲取遠程的通知服務(wù) 
  sService = INotificationManager.Stub.asInterface(ServiceManager.getService("notification")); 
  return sService; 
 } 
 
 
 //TN是一個瞬態(tài)通知的子類,里面包含顯示和隱藏兩個任務(wù)對象 
 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任務(wù)到執(zhí)行線程中 
   */ 
  @Override 
  public void show() { 
   if (localLOGV) Log.v(TAG, "SHOW: " + this); 
   //handler發(fā)送異步任務(wù)了 
   mHandler.post(mShow); 
  } 
 
 
  /** 
   * 同上 
   */ 
  @Override 
  public void hide() { 
   if (localLOGV) Log.v(TAG, "HIDE: " + this); 
   mHandler.post(mHide); 
  } 
 
  //... 
 } 
 
} 

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

重點是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出多個變量的值或者其他任務(wù),可在操作手機時直觀可見。問題來了,彈出是無論我們的操作有多快,這些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ā)送異步任務(wù),調(diào)用遠程通知服務(wù)顯示通知,當然是排隊等待顯示了。

結(jié)束語

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

相關(guān)文章

最新評論

宿州市| 阿图什市| 瓦房店市| 蕉岭县| 两当县| 枞阳县| 石城县| 嵊泗县| 舟山市| 焦作市| 南城县| 岳阳市| 莒南县| 大连市| 东乡| 康乐县| 石景山区| 柳州市| 海宁市| 乌鲁木齐县| 太谷县| 德令哈市| 临漳县| 苗栗市| 安宁市| 平舆县| 濮阳市| 太谷县| 喀喇| 明溪县| 土默特左旗| 西华县| 桦南县| 同德县| 利津县| 苏尼特右旗| 宣化县| 隆子县| 水富县| 泰兴市| 大洼县|