Android超實用的Toast提示框優(yōu)化分享
前言
相信每位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為空,即沒有設置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 } } /** * 關(guān)閉一個正在顯示的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能有所幫助,如果有大家有疑問可以留言交流。
相關(guān)文章
Android中判斷手機是否聯(lián)網(wǎng)實例
這篇文章主要介紹了Android中判斷手機是否聯(lián)網(wǎng)實例,包括xml配置文件及功能代碼的實現(xiàn),需要的朋友可以參考下2014-10-10Android中WebView加載網(wǎng)頁設置進度條
這篇文章主要為大家詳細介紹了Android中WebView加載網(wǎng)頁設置進度條,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05Android 實現(xiàn)旋轉(zhuǎn)木馬的音樂效果
大家一定在百度音樂上在線聽過歌,有沒有注意到那個旋轉(zhuǎn)的唱片,本篇文章主要介紹在Android上如何實現(xiàn)這樣的功能,有需要的小伙伴可以參考下2016-07-07Android實現(xiàn)GridView中的item自由拖動效果
在前一個項目中,實現(xiàn)了一個功能是gridview中的item自由拖到效果,實現(xiàn)思路很簡單,主要工作就是交換節(jié)點,以及拖動時的移動效果,下面小編給大家分享具體實現(xiàn)過程,對gridview實現(xiàn)拖拽效果感興趣的朋友一起看看吧2016-11-11