編輯:關於Android編程
前言
相信每位Android開發者都用過Toast,都知道是彈出消息的。類似於js裡面的alert,C#裡面的MesageBox。當然android裡面也有dialog,dialog是有焦點的,可與用戶交互。而toast是沒有焦點的,時間到了自動消失,不能回應用戶的交互,下面就跟大家分享下Android中Toast提示框的優化方法。
先看下源碼:
public class Toast {
public static final int LENGTH_SHORT = 0;
public static final int LENGTH_LONG = 1;
/**
* 構造一個空的toast。你必須在調動show()之前,線調用setView()
* @param context 參數,application或者activity都可以
*/
public Toast(Context context) {
...
//獲取系統內置的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");
}
//通過系統服務,獲取通知管理器。看來這是使用系統通知?
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.
* 通常你不必調用它,在適當的時長後它會自動消失的。
*/
public void cancel() {
mTN.hide();
try {
getService().cancelToast(mContext.getPackageName(), mTN);
} catch (RemoteException e) {
// Empty
}
}
/**
* 設置toast顯示的視圖內容,不單單是黑色的界面,你可以自己決定顯示什麼
* @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;
}
/**
* 構造一個只包含一個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,可以去查看下內容
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()方法創建出來的toast對象顯示的文本內容
* @param s 待顯示的新文本內容.
*/
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,設置新文本內容
*/
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是一個瞬態通知的子類,裡面包含顯示和隱藏兩個任務對象
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;
}
};
//出現Handler了哦
final Handler mHandler = new Handler();
/**
* 調度handleShow任務到執行線程中
*/
@Override
public void show() {
if (localLOGV) Log.v(TAG, "SHOW: " + this);
//handler發送異步任務了
mHandler.post(mShow);
}
/**
* 同上
*/
@Override
public void hide() {
if (localLOGV) Log.v(TAG, "HIDE: " + this);
mHandler.post(mHide);
}
//...
}
}
通過上面的源碼解讀,了解到有遠程通知,handler異步任務等信息,不多說,自己看。
重點是toast的用法:
1、直接調用makeText靜態方法即可,返回的是Toast對象,最後別忘了調用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的方式創建,別忘了setView()方法:
Toast toast = new Toast(); toast.setView(view); toast.setText(s); toast.setGravity(gravity, xOffset, yOffset); toast.setDuration(duration); toast.show();
以上這些都不值得一提,很簡單。
在開發過程中,有這樣的需求:在項目總,我們偷懶,想連串toast出多個變量的值或者其他任務,可在操作手機時直觀可見。問題來了,彈出是無論我們的操作有多快,這些toast內容都是一個跟著一個顯示,沒辦法快進。哪怕我們玩完了,退出了app,它還在彈。怎麼辦?有沒有辦法讓toast的內容與我們的操作同步,快速反應?
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();
}
}
單例模式,每次創建toast都調用這個類的show方法,Toast的生命周期從show開始,到自己消失或者cancel為止。如果正在顯示,則修改顯示的內容,你持有這個對象的引用,當然可以修改顯示的內容了。若每次你都makeText或者new一個toast對象,即每次通過handler發送異步任務,調用遠程通知服務顯示通知,當然是排隊等待顯示了。
結束語
以上就是Android中Toast提示框優化的全部內容,希望對大家開發Android能有所幫助,如果有大家有疑問可以留言交流。
Android 界面跳轉及數據交換
本文演示:Android 界面跳轉及數據交換,通過一個小Demo展示全部過程。 效果如下所示: 1)MainActivity.java &n
android4.4 webview chromium顯示網頁的chromium內核結構
android4.4 webview chromium是單進程的,圖中所有組件都運行在Browser進程中。 按從上而下的順序介紹這張圖中與顯示網頁相關的chromiu
微信ohh是什麼意思 微信翻譯有趣表白代碼都有哪些
微信ohh是什麼意思?在微信聊天及朋友圈那裡都可以看到,小伙伴們在微信中輸入ohh,然後叫別人使用微信翻譯來翻譯這個詞會出現“留在我身邊&rdq
往android - Eclipse中導入新項目時,出現Remove @Override annotation錯誤提示
原因:編譯器版本的問題。Java 1.5的編譯器默認對父類的方法進行覆蓋,采用@Override進行說明;但1.6已經擴展到對接口的方法;所以如果還是以Java 1.5