Android 自定義Toast實現多次觸發只會顯示一次toast

wuden發表於2017-12-14

#使用場景描述 當我們處於某個場景,例如一個按鈕可以觸發toast的顯示,當你在多次點選按鈕時,會多次觸發toast的顯示。而呼叫android原生的toast的makeText的方式所生產的toast是被加到一個佇列裡面,然後依次執行。這樣就會出現多次點選按鈕觸發的toast一直會按佇列出現在介面上,而且即使退出了當前介面也會一直出現在手機上,而且無法手動取消,這時的使用者體驗變得非常的差。這時就可以使用自定義樣式的toast。

#效果展示

自定義toast展示效果

#自定義toast的好處 1.多次觸發也只顯示一個toast,只是把toast的內容替換成最新一次觸發需要顯示的內容。

2.可以手動取消toast的顯示(不過這個取消還是會帶有一點延遲效果,不是馬上就消失,而是漸漸的消失,該功能主要是通過單例來實現,一直保持只有一個toast例項,這樣在呼叫cancel方法時就能把toast給取消掉。)

3.可以自定義toast的樣式,這樣就可以做到定製的效果。

#使用方法 ####1.將自定義toast引入你們的工程中,以下是toast的原始碼。

public enum  CustomToast {
    INSTANCE;// 實現單例
    private Toast mToast;
    private TextView mTvToast;
    public void showToast(Context ctx, String content) {
        if (mToast == null) {
            mToast = new Toast(ctx);
            mToast.setGravity(Gravity.CENTER, 0, 0);//設定toast顯示的位置,這是居中
            mToast.setDuration(Toast.LENGTH_SHORT);//設定toast顯示的時長
            View _root = LayoutInflater.from(ctx).inflate(R.layout.toast_custom_common, null);//自定義樣式,自定義佈局檔案
            mTvToast = (TextView) _root.findViewById(R.id.tvCustomToast);
            mToast.setView(_root);//設定自定義的view 
       }
        mTvToast.setText(content);//設定文字
        mToast.show();//展示toast
    }
    public void showToast(Context ctx, int stringId) {
        showToast(ctx, ctx.getString(stringId));
    }
    public void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
            mToast = null;
            mTvToast = null;
        }
    }
}
複製程式碼

####2.需要的一些資原始檔 1)自定義樣式的佈局toast_custom_common,完全可以按照自己的需求進行佈局,以下是案例中的佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tvCustomToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/toast_custom_common_bg"
        android:gravity="center"
        android:minWidth="200dp"
        android:padding="15dp"
        android:textColor="@color/colorBasicWhite"
        android:textSize="17sp" />
</LinearLayout>
複製程式碼

2)資原始檔

<color name="colorBasicWhite">#ffffff</color>
<color name="colorAutoDismissToast">#88000000</color>
複製程式碼
//@drawable/toast_custom_common_bg
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="10dp" />
    <solid android:color="@color/colorAutoDismissToast" />
</shape>
複製程式碼

####3.在你對應的頁面中直接使用toast即可

public class TestCustomToastActivity extends AppCompatActivity {
    private int count = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_custom_toast);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btnShowToast,R.id.btnCancelToast})
    public void clickEvent(View v){
        switch (v.getId()){
            case R.id.btnShowToast:
                count++;
                CustomToast.INSTANCE.showToast(this,getString(R.string.custom_toast_click_number,count));
                break;
            case R.id.btnCancelToast:
                count = 0;
                CustomToast.INSTANCE.cancelToast();//可手動取消toast 
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CustomToast.INSTANCE.cancelToast();//銷燬頁面時,取消掉toast
    }
}
複製程式碼

相關文章