1 AppToast介紹
1.1 實(shí)現(xiàn)方式
全局只有一個(gè)Toast實(shí)例,每次調(diào)用show()方法顯示Toast前都要先取消上次的Toast顯示,然后顯示本次的消息。
首先創(chuàng)建一個(gè)名為AppToast的類,在里面定義一個(gè)全局靜態(tài)Toast對(duì)象和一個(gè)全局Application對(duì)象的弱引用。
private static Toast toast = null; // Global Toast
private static WeakReference<Application> app;
定義一個(gè)init()方法,用于得到用戶傳入的Application實(shí)例。
public static void init(Application application) {
app = new WeakReference<Application>(application);
}
封裝showToast()方法,方便調(diào)用。
/**
* 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();
}
也可以封裝一個(gè)getToast()方法用于得到Toast實(shí)例,允許我們?cè)O(shè)置其屬性,便于自定義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 使用方法
首先創(chuàng)建一個(gè)類繼承自Application,在其onCreate()方法中調(diào)用我們之前寫的init()方法進(jìn)行AppToast類的初始化。
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// 初始化AppToast庫(kù)
AppToast.init(this);
}
}
注意:不要忘記在AndroidManifest.xml文件中的application節(jié)點(diǎn)下配置android:name屬性。
<application
...
android:name=".MyApplication" >
<activity android:name=".MainActivity" >
...
</activity>
</application>
之后就可以在代碼中進(jìn)行使用了,比如:
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();
顯示效果如下圖:
開源庫(kù)、樣例工程、詳細(xì)文檔下載地址:
liying2008/ApplicationToast
該庫(kù)已上傳至jcenter倉(cāng)庫(kù),使用Android Studio可以通過在線依賴引用的方式引入該庫(kù)。
dependencies {
compile 'cc.duduhuo.applicationtoast:applicationtoast:0.3'
}
2 CusToast介紹
2.1 功能介紹
CusToast是一個(gè)具有即時(shí)顯示并且內(nèi)置了10種樣式的Toast工具庫(kù),現(xiàn)在簡(jiǎn)單介紹其實(shí)現(xiàn)原理。
在CusToast類中定義了一個(gè)枚舉類型Style,即Toast顯示的樣式。
public enum Style {
DEFAULT,
LIGHT_BLUE,
BLUE,
LIGHT_RED,
RED,
LIGHT_GREEN,
GREEN,
LIGHT_YELLOW,
YELLOW,
GRAY_1
}
為了方便對(duì)Toast對(duì)象進(jìn)行操作,我們創(chuàng)建一個(gè)自定義的Toast類,其繼承自Toast,方便我們擴(kuò)展Toast的功能,比如顯示帶圖片的Toast和顯示帶副標(biāo)題的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;
}
其余方法和布局文件請(qǐng)參考文末鏈接。
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一樣,在自己項(xiàng)目的Application類中初始化CusToast庫(kù),方法也和AppToast類似。
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// 初始化CusToast庫(kù)(兩種方式選其一)
// 方式1:初始化同時(shí)指定CusToast的默認(rèn)顯示樣式
CusToast.init(this, CusToast.Style.RED);
// 方式2:初始化,使用默認(rèn)顯示樣式
// CusToast.init(this);
}
}
之后就可以在代碼中進(jìn)行使用了,比如:
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的幾種內(nèi)置樣式。
| Style | 預(yù)覽 |
|---|---|
| DEFAULT | ![]() DEFAULT
|
| LIGHT_BLUE | ![]() LIGHT_BLUE
|
| LIGHT_RED | ![]() LIGHT_RED
|
| RED | ![]() RED
|
| LIGHT_GREEN | ![]() LIGHT_GREEN
|
| GREEN | ![]() GREEN
|
| LIGHT_YELLOW | ![]() LIGHT_YELLOW
|
| YELLOW | ![]() YELLOW
|
| GRAY_1 | ![]() GRAY_1
|
其他樣式
| 樣式 | 預(yù)覽 |
|---|---|
| CusToastWithSub | ![]() CusToastWithSub
|
| CusToastWithIcon | ![]() CusToastWithIcon
|
開源庫(kù)、樣例工程、詳細(xì)文檔下載地址:
liying2008/CusToast
該庫(kù)已上傳至jcenter倉(cāng)庫(kù),使用Android Studio可以通過在線依賴引用的方式引入該庫(kù)。
dependencies {
compile 'cc.duduhuo.custoast:custoast:0.2'
}










