標籤:知識 resid 介面 content class new stat 不顯示 tran
前言:
一直在考慮寫一下Android實際項目中的一些總結,翻看CSDN部落格,上一篇已經是一年多曾經。
本系列定位Android基礎工具類重構。旨在記錄實際項目中經經常使用到的一些工具類,比方Toast、Dialog、動畫類,ImageLoader類等等。正在梳理,但發現梳理完再寫預計黃花菜都涼了。所以改變策略,邊寫邊梳理。
首先要寫的就是這個Toast。
一、說明
作為Android系統提供的基類,Toast是最簡單的提示訊息類。特點懸浮。跨介面(Activity)特定時間內自己主動銷毀。
二、簡單使用
Toast.makeText(getApplicationContext(), "你想提示的資訊",Toast.LENGTH_SHORT).show();
// 特別說一下:查看原始碼會發現makeText方法會返回一個Toast執行個體,例如以下:
// 由於每次都會new一個新的Toast,這也就是為什麼假設同一時候間多次調用makeText會彈出多個提示框。直到全部的提示完畢才消失
原始碼:
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); 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; }
三、複雜使用
我們看到原始碼。用到了layout組件。那也就是說我們也能夠定義自己的View,Toast提供了在螢幕上的顯示位置。
這樣我們就能夠自己定義自己的樣式的而且在須要的位置顯示的Toast。見四
四、實際項目中使用
4.1、相關知識點
1)在實際項目中Toast樣式是一致的,也就是說整個APP生命週期內僅僅須要一個Toast執行個體就可以
2)Toast中使用的Context直接使用Appliction中的Context就可以。
由於APP中Context的數量=1個Application + n*Activity的數量 + m*Service的數量
4.2、會遇到的問題
避免上面所說的多次反覆彈出Toast,所以我們將會推斷Toast執行個體是否存在,假設存在直接使用。假設不存在才new
4.3、效果
本例中使用的是在介面頂部彈出自己定義的Toast。假設成功彈出綠色提示條,失敗彈出黃色提示條
4.4、話不多說,上代碼
package com.ray.utils;import android.content.Context;import android.content.res.Resources;import android.os.Handler;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.widget.TextView;import android.widget.Toast;import com.ray.R;import com.ray.app.utils.ApplicationUtil;/** * User: Ray * Date: 16/3/3 * ReadMe: Toast-工具類 */public class ToastUtil { private static Context context = BaseApplication.getInstance();// App生命週期中唯一Context。BaseApplication繼承Application private static LayoutInflater inflater = LayoutInflater.from(context);// 布局載入 private static View myToastView = inflater.inflate(R.layout.layout_top_toast, null); private static TextView msgView = (TextView) myToastView.findViewById(R.id.tv_msg_text); private static final int TYPE_CODE_SUCCESS = 0x01; private static final int TYPE_CODE_ERROR = 0x02; private static final int COLOR_SUCCESS = context.getResources().getColor(R.color.msg_status_success); private static final int COLOR_ERROR = context.getResources().getColor(R.color.msg_status_warn); private static final int DEFAULT_TIME_DELAY = 50;// 單位:毫秒 private static Toast toast;// 系統提示類 private static Handler handler; public static void showSuccessMsg(int msgResId) { try { showSuccessMsg(context.getString(msgResId)); } catch (Resources.NotFoundException e) { e.printStackTrace(); } } public static void showErrorMsg(int msgResId) { try { showErrorMsg(context.getString(msgResId)); } catch (Resources.NotFoundException e) { e.printStackTrace(); } } public static void showSuccessMsg(String msg) { showMsg(TYPE_CODE_SUCCESS, msg); } public static void showErrorMsg(String msg) { showMsg(TYPE_CODE_ERROR, msg); } private static void showMsg(final int typeCode, final String msg) { if (context == null// || !ApplicationUtil.isRunningForeground(context)// 假設APP回到後台,則不顯示 || msg == null) { return; } if (toast == null) {// 防止反覆提示:不為Null,即全域使用同一個Toast執行個體 toast = new Toast(context); } if (handler == null) { handler = new Handler(); } handler.postDelayed(new Runnable() { @Override public void run() { int msgViewBagColor = 0; switch (typeCode) { case TYPE_CODE_SUCCESS: msgViewBagColor = COLOR_SUCCESS; break; case TYPE_CODE_ERROR: msgViewBagColor = COLOR_ERROR; break; default: msgViewBagColor = COLOR_SUCCESS; break; } msgView.setBackgroundColor(msgViewBagColor); msgView.setText(msg); toast.setView(myToastView); toast.setGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);// 頂部置中 toast.setDuration(Toast.LENGTH_SHORT); toast.show(); } }, DEFAULT_TIME_DELAY); } // 暫不正確外提供:主要針對須要在某個時候,取消提示 private static void cancelToast() { if (toast != null) { toast.cancel(); toast = null; } }}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:clickable="false" android:focusable="false"> <TextView android:id="@+id/tv_msg_text" android:layout_width="match_parent" android:layout_height="@dimen/nav_height" android:background="@color/msg_status_success" android:ellipsize="end" android:gravity="center" android:maxLines="2" android:paddingLeft="15dp" android:paddingRight="15dp" android:textColor="@color/white" android:textSize="16dp" /></RelativeLayout>
五、Android5.0官方新替代組件
眼下市面上未見有太多使用,興許補充。
。。
Android基礎工具類重構系列一Toast