Android超實(shí)用的Toast提示框優(yōu)化分享
前言
相信每位Android開發(fā)者都用過Toast,都知道是彈出消息的。類似于js里面的alert,C#里面的MesageBox。當(dāng)然android里面也有dialog,dialog是有焦點(diǎn)的,可與用戶交互。而toast是沒有焦點(diǎn)的,時(shí)間到了自動(dòng)消失,不能回應(yīng)用戶的交互,下面就跟大家分享下Android中Toast提示框的優(yōu)化方法。
先看下源碼:
public class Toast {
public static final int LENGTH_SHORT = 0;
public static final int LENGTH_LONG = 1;
/**
* 構(gòu)造一個(gè)空的toast。你必須在調(diào)動(dòng)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);
}
/**
* 在指定的時(shí)長顯示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)閉一個(gè)正在顯示的toast, 或者取消一個(gè)未顯示的toast.
* 通常你不必調(diào)用它,在適當(dāng)?shù)臅r(shí)長后它會(huì)自動(dòng)消失的。
*/
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è)置時(shí)長,只能是下面這兩個(gè)常量值,沒什么卵用
* @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)造一個(gè)只包含一個(gè)TextView的標(biāo)準(zhǔn)toast對象
*
* @param context 通常是application或者activity對象
* @param text 用于顯示的文本,可以是formatted text.
* @param duration 顯示時(shí)長. 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);
//包含了一個(gè)默認(rèn)的TextView,這個(gè)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標(biāo)簽的id是R.id.message。拿到這個(gè)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;
}
//獲取遠(yuǎn)程的通知服務(wù)
sService = INotificationManager.Stub.asInterface(ServiceManager.getService("notification"));
return sService;
}
//TN是一個(gè)瞬態(tài)通知的子類,里面包含顯示和隱藏兩個(gè)任務(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);
}
//...
}
}
通過上面的源碼解讀,了解到有遠(yuǎn)程通知,handler異步任務(wù)等信息,不多說,自己看。
重點(diǎn)是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ā)過程中,有這樣的需求:在項(xiàng)目總,我們偷懶,想連串toast出多個(gè)變量的值或者其他任務(wù),可在操作手機(jī)時(shí)直觀可見。問題來了,彈出是無論我們的操作有多快,這些toast內(nèi)容都是一個(gè)跟著一個(gè)顯示,沒辦法快進(jìn)。哪怕我們玩完了,退出了app,它還在彈。怎么辦?有沒有辦法讓toast的內(nèi)容與我們的操作同步,快速反應(yīng)?
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)用這個(gè)類的show方法,Toast的生命周期從show開始,到自己消失或者cancel為止。如果正在顯示,則修改顯示的內(nèi)容,你持有這個(gè)對象的引用,當(dāng)然可以修改顯示的內(nèi)容了。若每次你都makeText或者new一個(gè)toast對象,即每次通過handler發(fā)送異步任務(wù),調(diào)用遠(yuǎn)程通知服務(wù)顯示通知,當(dāng)然是排隊(duì)等待顯示了。
結(jié)束語
以上就是Android中Toast提示框優(yōu)化的全部內(nèi)容,希望對大家開發(fā)Android能有所幫助,如果有大家有疑問可以留言交流。
- android 彈出提示框的使用(圖文實(shí)例)
- Android使用Toast顯示消息提示框
- android實(shí)現(xiàn)彈出提示框
- Android仿QQ、微信聊天界面長按提示框效果
- Android編程之自定義AlertDialog(退出提示框)用法實(shí)例
- Android仿IOS自定義AlertDialog提示框
- Android仿百度谷歌搜索自動(dòng)提示框AutoCompleteTextView簡單應(yīng)用示例
- Android中仿IOS提示框的實(shí)現(xiàn)方法
- Android模擬美團(tuán)客戶端進(jìn)度提示框
- Android模仿Toast實(shí)現(xiàn)提示框效果
相關(guān)文章
Android中判斷手機(jī)是否聯(lián)網(wǎng)實(shí)例
這篇文章主要介紹了Android中判斷手機(jī)是否聯(lián)網(wǎng)實(shí)例,包括xml配置文件及功能代碼的實(shí)現(xiàn),需要的朋友可以參考下2014-10-10
Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
Android 實(shí)現(xiàn)旋轉(zhuǎn)木馬的音樂效果
大家一定在百度音樂上在線聽過歌,有沒有注意到那個(gè)旋轉(zhuǎn)的唱片,本篇文章主要介紹在Android上如何實(shí)現(xiàn)這樣的功能,有需要的小伙伴可以參考下2016-07-07
Android實(shí)現(xiàn)GridView中的item自由拖動(dòng)效果
在前一個(gè)項(xiàng)目中,實(shí)現(xiàn)了一個(gè)功能是gridview中的item自由拖到效果,實(shí)現(xiàn)思路很簡單,主要工作就是交換節(jié)點(diǎn),以及拖動(dòng)時(shí)的移動(dòng)效果,下面小編給大家分享具體實(shí)現(xiàn)過程,對gridview實(shí)現(xiàn)拖拽效果感興趣的朋友一起看看吧2016-11-11

