1 AppToast介紹
1.1 實現方式
全局只有一個Toast實例,每次調用show()方法顯示Toast前都要先取消上次的Toast顯示,然后顯示本次的消息。
首先創建一個名為AppToast的類,在里面定義一個全局靜態Toast對象和一個全局Application對象的弱引用。
private static Toast toast = null; // Global Toast
private static WeakReference<Application> app;
定義一個init()方法,用於得到用戶傳入的Application實例。
public static void init(Application application) {
app = new WeakReference<Application>(application);
}
封裝showToast()方法,方便調用。
/**
* Display Toast
*
* @param resId The resource id of the string resource to use. Can be formatted text.
*/
public static void showToast(@StringRes int resId) {
if (toast != null) {
toast.cancel();
toast = null;
}
toast = Toast.makeText(app.get(), resId, LENGTH_SHORT);
toast.show();
}
也可以封裝一個getToast()方法用於得到Toast實例,允許我們設置其屬性,便於自定義Toast顯示的效果。
/**
* Get a Toast object <br>
* Need to call show() method to be displayed
*
* @return Toast object.
*/
public static Toast getToast() {
if (toast != null) {
toast.cancel();
toast = null;
}
toast = Toast.makeText(app.get(), "", Toast.LENGTH_SHORT);
return toast;
}
1.2 使用方法
首先創建一個類繼承自Application,在其onCreate()方法中調用我們之前寫的init()方法進行AppToast類的初始化。
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// 初始化AppToast庫
AppToast.init(this);
}
}
注意:不要忘記在AndroidManifest.xml文件中的application節點下配置android:name屬性。
<application
...
android:name=".MyApplication" >
<activity android:name=".MainActivity" >
...
</activity>
</application>
之后就可以在代碼中進行使用了,比如:
AppToast.showToast(R.string.toast2);
和
Toast toast = AppToast.getToast();
toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 0);
toast.setText("自定義Toast");
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
顯示效果如下圖:

開源庫、樣例工程、詳細文檔下載地址:
liying2008/ApplicationToast
該庫已上傳至jcenter倉庫,使用Android Studio可以通過在線依賴引用的方式引入該庫。
dependencies {
compile 'cc.duduhuo.applicationtoast:applicationtoast:0.3'
}
2 CusToast介紹
2.1 功能介紹
CusToast是一個具有即時顯示並且內置了10種樣式的Toast工具庫,現在簡單介紹其實現原理。
在CusToast類中定義了一個枚舉類型Style,即Toast顯示的樣式。
public enum Style {
DEFAULT,
LIGHT_BLUE,
BLUE,
LIGHT_RED,
RED,
LIGHT_GREEN,
GREEN,
LIGHT_YELLOW,
YELLOW,
GRAY_1
}
為了方便對Toast對象進行操作,我們創建一個自定義的Toast類,其繼承自Toast,方便我們擴展Toast的功能,比如顯示帶圖片的Toast和顯示帶副標題的Toast。
通過向DToast類的setView()方法傳入樣式名,得到不同樣式的DToast。
/**
* Add a view to CusToast.
*
* @param application this application.
* @param style the style of CusToast.
* @return current instance.
*/
public DToast setView(Application application, CusToast.Style style) {
dView = View.inflate(application, R.layout.ddh_cus_toast, null);
dText = (TextView) dView.findViewById(R.id.dText);
setStyle(style);
super.setView(dView);
return this;
}
其余方法和布局文件請參考文末鏈接。
CusToast類中的showToast()方法如下所示。
/**
* Display Toast.
*
* @param text The resource id of the string resource to use. Can be formatted text.
*/
public static void showToast(@StringRes int text) {
clearToast();
toast = new DToast(app.get());
toast.setView(app.get(), defStyle);
toast.setText(text);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();
}
clearToast()方法如下,目的就是立即取消正在顯示的“舊”Toast。
/**
* Clear an existing CusToast.
*/
private static void clearToast() {
if (toast != null) {
toast.cancel();
toast = null;
}
}
2.2 使用方法
首先,和AppToast一樣,在自己項目的Application類中初始化CusToast庫,方法也和AppToast類似。
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// 初始化CusToast庫(兩種方式選其一)
// 方式1:初始化同時指定CusToast的默認顯示樣式
CusToast.init(this, CusToast.Style.RED);
// 方式2:初始化,使用默認顯示樣式
// CusToast.init(this);
}
}
之后就可以在代碼中進行使用了,比如:
CusToast.showToast("Toast 1");
CusToast.showToast("Toast 3", Toast.LENGTH_LONG, CusToast.Style.LIGHT_RED);
DToast toast = CusToast.getToast("自定義Toast");
toast.setCusToastGravity(Gravity.CENTER, 0, 0)
.setTextSize(16)
.setStyle(CusToast.Style.GRAY_1)
.setTextColor(Color.WHITE)
// .setBackground(R.mipmap.ic_launcher)
// .setBackgroundColor(0xffff3444)
.setCusToastDuration(Toast.LENGTH_SHORT)
.show();
在此列舉一下CusToast的幾種內置樣式。
| Style | 預覽 |
|---|---|
| DEFAULT | ![]() |
| LIGHT_BLUE | ![]() |
| BLUE | ![]() |
| LIGHT_RED | ![]() |
| RED | ![]() |
| LIGHT_GREEN | ![]() |
| GREEN | ![]() |
| LIGHT_YELLOW | ![]() |
| YELLOW | ![]() |
| GRAY_1 | ![]() |
其他樣式
| 樣式 | 預覽 |
|---|---|
| CusToastWithSub | ![]() |
| CusToastWithIcon | ![]() |
開源庫、樣例工程、詳細文檔下載地址:
liying2008/CusToast
該庫已上傳至jcenter倉庫,使用Android Studio可以通過在線依賴引用的方式引入該庫。
dependencies {
compile 'cc.duduhuo.custoast:custoast:0.2'
}












