自定義一個(gè)可以即時(shí)顯示的Toast的工具類庫(kù)

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'
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,745評(píng)論 25 709
  • 發(fā)條精靈 和著藍(lán)調(diào) 在窗口顫著芭蕾 老鼠大膽走出洞口 與貓一起 沐浴陽(yáng)光 如果排除 脆生的玻璃 被曬碎的可能性 那...
    741a45b23ef8閱讀 244評(píng)論 0 0
  • 清貨第一天去普羅旺世上班,騎著電動(dòng)車,早上路上人很多,人來人往,一路騎行了大約1小時(shí)左右,導(dǎo)航提示我距離,大約可以...
    馬駿輝閱讀 689評(píng)論 0 0
  • 春色正好的三月,桃花灼灼,櫻花粉嫩,梨花如雪,柳條抽了新枝,湖面映了春光,海風(fēng)柔了音調(diào)……一如往日,可卻再無當(dāng)日坐...
    玉無雙閱讀 384評(píng)論 0 0

友情鏈接更多精彩內(nèi)容