1. 程式人生 > >Android使用列舉單例實現Toast快速重新整理(自定義吐司)

Android使用列舉單例實現Toast快速重新整理(自定義吐司)

通常我們使用Toast的時候可能會直接這樣寫:

Toast.makeText(context, text, duration).show();

這樣寫通常會有這樣一個問題:新Toast要等上一次Toast結束後才能顯示出來 

 這裡寫圖片描述

1.在MyApplication中建立Toast例項 



public class MyApplication extends Application{
    @Override
    public void onCreate() {
        super.onCreate();
        ToastMgr.builder.init(getApplicationContext());
    }

    public enum ToastMgr{
        builder;
        private View view;
        private TextView tv;
        private Toast toast;

        /**
         * 初始化Toast
         * @param context
         */
        public void init(Context context){
            view = LayoutInflater.from(context).inflate(R.layout.toast_view, null);
            tv = (TextView) view.findViewById(R.id.toast_textview);
            toast = new Toast(context);
            toast.setView(view);
        }
        /**
         * 顯示Toast
         * @param content
         * @param duration Toast持續時間
         */
        public void display(CharSequence content , int duration){
            if (content.length()!=0) {
                tv.setText(content);
                toast.setDuration(duration);
               //動態設定toast顯示的位置
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        }
    }
}

2.自定義toast的佈局檔案:

<?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="match_parent">
    <!--可以任意調整吐司的顯示位置-->
    <TextView
        android:id="@+id/toast_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/shape_radaus"
        android:padding="15dp"
        android:text="呵呵" />
</RelativeLayout>

3.shape檔案

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- rectangle表示為矩形 -->

    <!-- 填充的顏色 -->
    <solid android:color="@android:color/holo_blue_bright" />

    <!-- 邊框的顏色和粗細 -->
    <stroke
        android:width="1dp"
        android:color="@android:color/darker_gray"
        />

    <!-- android:radius 圓角的半徑 -->
    <corners
        android:radius="10dp"
        />

</shape>

4.寫一個工具類Utils

package com.githang.stepview.demo;

import android.widget.Toast;

/**
 * Created by dingxujun on 2018/12/10.
 *
 * @project StepView-master
 */
public class Utils {
    /**
     * 顯示toast
     *
     * @param content  內容
     * @param duration 持續時間
     */
    public static void toast(String content, int duration) {
        if (content == null) {
            return;
        } else {
            MyApplication.ToastMgr.builder.display(content, duration);
        }
    }

    /**
     * 顯示預設持續時間為short的Toast
     *
     * @param content 內容
     */
    public static void toast(String content) {
        if (content == null) {
            return;
        } else {
            MyApplication.ToastMgr.builder.display(content, Toast.LENGTH_SHORT);
        }
    }
}

5.可以在BaseActivity中可以進一步封裝,用著方便

我們在Activity中使用Toast的機率是非常大的,所以我們在Activity中每次使用Toast的時候都要Utils.toast() , 這樣還是有點麻煩,所以我們可以在BaseActivity中進一步封裝一下

package com.githang.stepview.demo;

import android.support.v7.app.AppCompatActivity;

/**
 * Created by dingxujun on 2018/12/10.
 *
 * @project StepView-master
 */
public class BaseActivity extends AppCompatActivity {
    public void toast(String content) {
        Utils.toast(content);
    }

    public void toast(String content, int duration) {
        Utils.toast(content, duration);
    }

}

這樣就非常方便了,我們可以看看Toast是否可以快速重新整理 

6.測試一下

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.githang.stepview.demo.R;

public class MyActivity extends BaseActivity implements OnClickListener {
    private Button mButton1;
    private Button mButton2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);
        initViews();
        registerListener();
    }

    private void registerListener() {
        mButton1.setOnClickListener(this);
        mButton2.setOnClickListener(this);
    }

    private void initViews() {
        mButton1 = (Button) findViewById(R.id.button1);
        mButton2 = (Button) findViewById(R.id.button2);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                toast("Button1");
                break;
            case R.id.button2:
                toast("Button2");
                break;
        }
    }
}

這裡寫圖片描述