天天看點

吐司工具類--ToastUtil

/**
 * Created by peng on 2015/6/15.
 * 吐司工具類
 */
public class ToastUtil {
    private static Toast TOAST;
    private static final String TAG = "ToastUtil";


    //短時間吐司
    public static void show(Context context, int resourceID) {
        show(context, resourceID, Toast.LENGTH_SHORT);
    }

    //短時間吐司
    public static void show(Context context, String text) {
        show(context, text, Toast.LENGTH_SHORT);
    }

    //自定義時長吐司
    public static void show(Context context, Integer resourceID, int duration) {
        String text = context.getResources().getString(resourceID);// 用于顯示的文字
        show(context, text, duration);
    }

    //自定義時長吐司
    public static void show(@NonNull final Context context, @NonNull final String text, final int duration) {

            if (TOAST == null) {
                TOAST = Toast.makeText(context, text, duration);
            } else {
                TOAST.setText(text);
                TOAST.setDuration(duration);
            }

            TOAST.show();
    }
}           

繼續閱讀