Toast util

customer_toast_bg

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners
        android:radius="30dp"/>
    <solid
        android:color="#88000000"/>

</shape>

R.layout.customer_toast

?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/customer_toast_bg"
    android:gravity="center"
    android:paddingBottom="10dp"
    android:paddingLeft="30dp"
    android:paddingRight="30dp"
    android:paddingTop="10dp">


    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"
        android:shadowColor="#BB000000"
        android:shadowRadius="2.75"
        android:textColor="#FFFFFF" />

</FrameLayout>




public class CustomerToast {

    private static Toast mToast;
    private static final String TAG = "CustomerToast";

    private static Toast initToast(Context context, CharSequence text, int duration) {
        View v = initView(context, text.toString());
        Toast toast = new Toast(context.getApplicationContext());
        toast.setDuration(duration);
        toast.setView(v);
        toast.setGravity(Gravity.CENTER, 0, 0);
        return toast;
    }

    public static void show(final Context context, final CharSequence text, final int duration) {
        if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
            showInUIThread(context, text, duration);
        } else {
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    showInUIThread(context, text, duration);
                }
            });
        }
    }

    private static void showInUIThread(Context context, CharSequence text, int duration) {
        if (!TextUtils.isEmpty(text)) {
            if (mToast == null) {
                LogUtil.d(TAG, "mToast == null");
                mToast = initToast(context, text, duration);
                mToast.show();
            } else {
                LogUtil.d(TAG, "mToast != null");
                mToast.cancel();
                mToast = initToast(context, text, duration);
                // View v = initView(context, text.toString());
                // mToast.setView(v);
                mToast.show();
            }
        } else {
            LogUtil.e(TAG, "text is empty");
        }
    }

    public static void show(Context context, int textId, int duration) {
        String text = context.getString(textId);
        if (!TextUtils.isEmpty(text)) {
            if (mToast == null) {
                LogUtil.d(TAG, "mToast == null");
                mToast = initToast(context, text, duration);
                mToast.show();
            } else {
                LogUtil.d(TAG, "mToast != null");
                mToast = initToast(context, text, duration);
                mToast.show();
            }
        } else {
            LogUtil.e(TAG, "text is empty");
        }
    }

    private static View initView(Context context, String text) {
        View v = LayoutInflater.from(context).inflate(R.layout.customer_toast, null);
        TextView textView = (TextView) v.findViewById(R.id.message);
        textView.setText(text);
        return v;
    }
}

public class ToastUtil {

    public static void toastNetworkError() {
        toastNormal(ASApplication.getInstance(), ASApplication.getInstance().getString(R.string.network_error));
    }

    public static void toastNormal(Context context, String content) {
        CustomerToast.show(context.getApplicationContext(), content, Toast.LENGTH_SHORT);
    }

    public static void toastLong(Context context, String content) {
        CustomerToast.show(context, content, Toast.LENGTH_LONG);
    }

    public static void toastLongNormal(Context context, String content) {
        CustomerToast.show(context, content, Toast.LENGTH_LONG);
    }

    public static void toastNormal(String content) {
        CustomerToast.show(ASApplication.getAppContext(), content, Toast.LENGTH_SHORT);
    }

    public static void toastNormal(int content) {
        CustomerToast.show(ASApplication.getAppContext(), content, Toast.LENGTH_SHORT);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值