Android开发工具类之Toast

Toast遇到一次写一次好麻烦,所以弄成一个工具类,调用方便点。

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.wsp.thinkpad.aa.R;

/**
 * Created by ${吴心良}
 * on 2017/2/17.
 * description:
 */

public class ToastUtils {
    private static Toast toast;


    public static void showToast(final Context context, final String message, final int duration) {
        toast = Toast.makeText(context.getApplicationContext(), message, duration);
        toast.show();
    }
    public static void showIconToast(Context context, String textId, int iconId, int colorId) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.toast, null);
        ((TextView) layout).setText(textId);
        ((TextView) layout).setTextColor(context.getResources().getColor(colorId));
        ((TextView) layout).setCompoundDrawablesWithIntrinsicBounds(iconId, 0, 0, 0);
        Toast toast = new Toast(context);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(layout);
        toast.show();
    }

    public static void showLongToast(final Context context, final String message) {
        showToast(context, message, Toast.LENGTH_LONG);
    }

    public static void showShortToast(final Context context, final String message) {
        showToast(context, message, Toast.LENGTH_SHORT);
    }
}

下面是xml代码 想怎么设置就怎么设置

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:drawablePadding="12dp"
    android:padding="12dp"
    android:gravity="center"
    android:background="#999999">

</TextView>

参考了下这个:https://my.oschina.net/banxi/blog/56007

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值