Android toast

系统默认方法

Android中Toast做为一个很好的用户交互的工具,在很多应用中都有应用。
系统默认的Toast十分简洁,使用也非常的简单。但是普通的Toast样式很单一,只能用于显示文字。

系统使用方法:

     Toast.makeText(context,"message",Toast.LENGTH_SHORT).show();

注意:不要忘了.show(),否则不显示

自定义Toast

先看效果图
Toas

public class ToastUtil
{
    private static Toast toast;

    private static final Handler M_HANDLER = new Handler(Looper.getMainLooper());

    /**
     * description 显示位置与现实时间的常量
     * param    SHOW_TOP        顶部
     * param    SHOW_CENTER     中间
     * param    SHOW_LEFT       左边
     * param    SHOW_RIGHT      右边
     * param    SHOW_BOTTOM     底部
     * param    SHOW_SHORT      显示短
     * param    SHOW_LONG       显示长
     * return
     */
    public static final int SHOW_TOP = Gravity.TOP;
    public static final int SHOW_CENTER = Gravity.CENTER;
    public static final int SHOW_START = Gravity.START;
    public static final int SHOW_END = Gravity.END;
    public static final int SHOW_BOTTOM = Gravity.BOTTOM;

    public static final int SHOW_SHORT = Toast.LENGTH_SHORT;
    public static final int SHOW_LONG = Toast.LENGTH_LONG;


    /**
     * description  默认显示Toast
     * param    context         context
     * param    message         显示的文字
     * param    bgResourceId    背景资源
     * param    iconResourceId  图标资源
     * param    iconPosition    图标位置
     * param    position        Toast位置
     * param    duration        显示时间
     * return
     */
    public static void showMsg(Context context, String message)
    {
        showToastUtil(context, message, 0, 0, 0);
    }

    public static void showMsg(Context context, String message, int bgResourceId)
    {
        showToastUtil(context, message, bgResourceId, 0, 0);
    }

    public static void showMsg(Context context, String message, int bgResourceId, int position, int duration)
    {
        showToastUtil(context, message, bgResourceId, position, duration);
    }

    public static void showImg(Context context, String message, int bgResourceId, int iconResourceId, int iconPosition, int position, int duration)
    {
        showImgToastUtil(context, message, bgResourceId, iconResourceId, iconPosition, position, duration);
    }

    /**
     * description Toast显示工具,下同
     * param    mContext   context
     * param    position   位置
     * param    resourceId 资源
     * param    text       内容
     * param    duration   时间
     * return
     * Author JetQiao
     * Time 2019/12/27 0027 17:15
     */
    private static void showToastUtil(Context mContext, final String text, int resourceId, int position, final int duration)
    {
        if (Looper.myLooper() == Looper.getMainLooper())
        {
            showUtils(mContext, text, resourceId, position, duration);
        } else
        {
            M_HANDLER.post(() -> showUtils(mContext, text, resourceId, position, duration));
        }
    }


    private static void showImgToastUtil(Context mContext, final String text, int resourceId, int iconResourceId, int iconPosition, int position, final int duration)
    {
        if (Looper.myLooper() == Looper.getMainLooper())
        {
            showImgUtils(mContext, text, resourceId, iconResourceId, iconPosition, position, duration);
        } else
        {
            M_HANDLER.post(() -> showImgUtils(mContext, text, resourceId, iconResourceId, iconPosition, position, duration));
        }
    }


    /**
     * description  显示带位置的Toast
     * param    mContext    context
     * param    text        显示的内容
     * param    resourceId  资源ID
     * param    position    显示位置
     * param    duration    显示时间
     * return
     */
    private static void showUtils(Context mContext, String text, int resourceId, int position, int duration)
    {
        if (toast != null)
        {
            toast.cancel();
        }
        toast = new Toast(mContext);

        @SuppressLint("InflateParams") View toastView = LayoutInflater.from(mContext).inflate(R.layout.toast_text_message, null);
        LinearLayout llToastTextMeg = toastView.findViewById(R.id.ll_toast_text_msg);
        if (resourceId == 0)
        {
            llToastTextMeg.setBackgroundResource(R.drawable.shape_tips_toast_default);
        } else
        {
            llToastTextMeg.setBackgroundResource(resourceId);
        }
        TextView tvToastMsg = toastView.findViewById(R.id.tv_toast_text_msg);
        //要提示的文本
        tvToastMsg.setText(text);
        //位置
        switch (position)
        {
            case SHOW_TOP:
                toast.setGravity(SHOW_TOP, 0, 0);
                break;

            case SHOW_START:
                toast.setGravity(SHOW_START, 0, 0);
                break;

            case SHOW_END:
                toast.setGravity(SHOW_END, 0, 0);
                break;

            case SHOW_CENTER:
                toast.setGravity(SHOW_CENTER, 0, 0);
                break;

            case SHOW_BOTTOM:
                toast.setGravity(SHOW_BOTTOM, 0, 0);
                break;
            default:
                toast.setGravity(SHOW_BOTTOM, 0, 80);
                break;
        }
        //设置短暂提示
        toast.setDuration(duration);
        //把定义好的View布局设置到Toast里面
        toast.setView(toastView);
        toast.show();
    }

    /**
     * description  显示带图片位置的Toast
     * param
     * return
     */
    private static void showImgUtils(Context mContext, String text, int resourceId, int iconResourceId, int iconPosition, int position, int duration)
    {
        if (toast != null)
        {
            toast.cancel();
        }
        toast = new Toast(mContext);
        @SuppressLint("InflateParams") View toastView = LayoutInflater.from(mContext).inflate(R.layout.toast_img_message, null);
        LinearLayout llToastImgMeg = toastView.findViewById(R.id.ll_toast_img_msg);
        if (resourceId == 0)
        {
            llToastImgMeg.setBackgroundResource(R.drawable.shape_tips_toast_default);
        } else
        {
            llToastImgMeg.setBackgroundResource(resourceId);
        }
        ImageView imgToastImgTop = toastView.findViewById(R.id.img_toast_img_msg_top);
        ImageView imgToastImgLeft = toastView.findViewById(R.id.img_toast_img_msg_left);
        ImageView imgToastImgRight = toastView.findViewById(R.id.img_toast_img_msg_right);
        ImageView imgToastImgBottom = toastView.findViewById(R.id.img_toast_img_msg_bottom);
        imgToastImgTop.setImageResource(iconResourceId);
        imgToastImgLeft.setImageResource(iconResourceId);
        imgToastImgRight.setImageResource(iconResourceId);
        imgToastImgBottom.setImageResource(iconResourceId);
        switch (iconPosition)
        {
            case SHOW_TOP:
                imgToastImgTop.setVisibility(View.VISIBLE);
                imgToastImgLeft.setVisibility(View.GONE);
                imgToastImgRight.setVisibility(View.GONE);
                imgToastImgBottom.setVisibility(View.GONE);
                break;
            case SHOW_END:
                imgToastImgTop.setVisibility(View.GONE);
                imgToastImgLeft.setVisibility(View.GONE);
                imgToastImgRight.setVisibility(View.VISIBLE);
                imgToastImgBottom.setVisibility(View.GONE);
                break;
            case SHOW_BOTTOM:
                imgToastImgTop.setVisibility(View.GONE);
                imgToastImgLeft.setVisibility(View.GONE);
                imgToastImgRight.setVisibility(View.GONE);
                imgToastImgBottom.setVisibility(View.VISIBLE);
                break;
            case SHOW_START:
            default:
                imgToastImgTop.setVisibility(View.GONE);
                imgToastImgLeft.setVisibility(View.VISIBLE);
                imgToastImgRight.setVisibility(View.GONE);
                imgToastImgBottom.setVisibility(View.GONE);
                break;
        }
        TextView tvToastImgMsg = toastView.findViewById(R.id.tv_toast_img_msg);
        tvToastImgMsg.setText(text);
        //Toast位置
        switch (position)
        {
            case SHOW_TOP:
                toast.setGravity(SHOW_TOP, 0, 0);
                break;
            case SHOW_START:
                toast.setGravity(SHOW_START, 0, 0);
                break;
            case SHOW_END:
                toast.setGravity(SHOW_END, 0, 0);
                break;
            case SHOW_CENTER:
                toast.setGravity(SHOW_CENTER, 0, 0);
                break;
            case SHOW_BOTTOM:
                toast.setGravity(SHOW_BOTTOM, 0, 0);
                break;
            default:
                toast.setGravity(SHOW_BOTTOM, 0, 80);
                break;
        }
        toast.setDuration(duration);
        toast.setView(toastView);
        toast.show();
    }
}

说明

使用方法:

 ToastUtil.showMsg(context, "默认显示");
 
 ToastUtil.showMsg(context, "显示在上面",0, ToastUtil.SHOW_TOP, 0);
 
 ToastUtil.showImg(context,"左侧图标",0,R.mipmap.icon_toast_tips,ToastUtil.SHOW_START,0,0);

方法参数:

不带icon的toast
 /**
     * description  显示带位置的Toast
     * param    mContext    context
     * param    text        显示的内容
     * param    resourceId  资源ID
     * param    position    显示位置
     * param    duration    显示时间
     * return
     */
    private static void showUtils(Context mContext, String text, int resourceId, int position, int duration)
    {
        if (toast != null)
        {
            toast.cancel();
        }
        toast = new Toast(mContext);

        @SuppressLint("InflateParams") View toastView = LayoutInflater.from(mContext).inflate(R.layout.toast_text_message, null);
        LinearLayout llToastTextMeg = toastView.findViewById(R.id.ll_toast_text_msg);
        if (resourceId == 0)
        {
            llToastTextMeg.setBackgroundResource(R.drawable.shape_tips_toast_default);
        } else
        {
            llToastTextMeg.setBackgroundResource(resourceId);
        }
        TextView tvToastMsg = toastView.findViewById(R.id.tv_toast_text_msg);
        //要提示的文本
        tvToastMsg.setText(text);
        //位置
        switch (position)
        {
            case SHOW_TOP:
                toast.setGravity(SHOW_TOP, 0, 0);
                break;

            case SHOW_START:
                toast.setGravity(SHOW_START, 0, 0);
                break;

            case SHOW_END:
                toast.setGravity(SHOW_END, 0, 0);
                break;

            case SHOW_CENTER:
                toast.setGravity(SHOW_CENTER, 0, 0);
                break;

            case SHOW_BOTTOM:
                toast.setGravity(SHOW_BOTTOM, 0, 0);
                break;
            default:
                toast.setGravity(SHOW_BOTTOM, 0, 80);
                break;
        }
        //设置短暂提示
        toast.setDuration(duration);
        //把定义好的View布局设置到Toast里面
        toast.setView(toastView);
        toast.show();
    }
带icon的toast
 /**
     * description  显示带图片位置的Toast
     * param    mContext        context
     * param    text            message
     * param    resourceId      背景图片资源
     * param    iconResourceId  icon图片资源
     * param    iconPosition    icon图片位置
     * param    position        toast的位置
     * param    duration        显示时间
     * return
     */
    private static void showImgUtils(Context mContext, String text, int resourceId, int iconResourceId, int iconPosition, int position, int duration)
    {
        if (toast != null)
        {
            toast.cancel();
        }
        toast = new Toast(mContext);
        @SuppressLint("InflateParams") View toastView = LayoutInflater.from(mContext).inflate(R.layout.toast_img_message, null);
        LinearLayout llToastImgMeg = toastView.findViewById(R.id.ll_toast_img_msg);
        if (resourceId == 0)
        {
            llToastImgMeg.setBackgroundResource(R.drawable.shape_tips_toast_default);
        } else
        {
            llToastImgMeg.setBackgroundResource(resourceId);
        }
        ImageView imgToastImgTop = toastView.findViewById(R.id.img_toast_img_msg_top);
        ImageView imgToastImgLeft = toastView.findViewById(R.id.img_toast_img_msg_left);
        ImageView imgToastImgRight = toastView.findViewById(R.id.img_toast_img_msg_right);
        ImageView imgToastImgBottom = toastView.findViewById(R.id.img_toast_img_msg_bottom);
        imgToastImgTop.setImageResource(iconResourceId);
        imgToastImgLeft.setImageResource(iconResourceId);
        imgToastImgRight.setImageResource(iconResourceId);
        imgToastImgBottom.setImageResource(iconResourceId);
        switch (iconPosition)
        {
            case SHOW_TOP:
                imgToastImgTop.setVisibility(View.VISIBLE);
                imgToastImgLeft.setVisibility(View.GONE);
                imgToastImgRight.setVisibility(View.GONE);
                imgToastImgBottom.setVisibility(View.GONE);
                break;
            case SHOW_END:
                imgToastImgTop.setVisibility(View.GONE);
                imgToastImgLeft.setVisibility(View.GONE);
                imgToastImgRight.setVisibility(View.VISIBLE);
                imgToastImgBottom.setVisibility(View.GONE);
                break;
            case SHOW_BOTTOM:
                imgToastImgTop.setVisibility(View.GONE);
                imgToastImgLeft.setVisibility(View.GONE);
                imgToastImgRight.setVisibility(View.GONE);
                imgToastImgBottom.setVisibility(View.VISIBLE);
                break;
            case SHOW_START:
            default:
                imgToastImgTop.setVisibility(View.GONE);
                imgToastImgLeft.setVisibility(View.VISIBLE);
                imgToastImgRight.setVisibility(View.GONE);
                imgToastImgBottom.setVisibility(View.GONE);
                break;
        }
        TextView tvToastImgMsg = toastView.findViewById(R.id.tv_toast_img_msg);
        tvToastImgMsg.setText(text);
        //Toast位置
        switch (position)
        {
            case SHOW_TOP:
                toast.setGravity(SHOW_TOP, 0, 0);
                break;
            case SHOW_START:
                toast.setGravity(SHOW_START, 0, 0);
                break;
            case SHOW_END:
                toast.setGravity(SHOW_END, 0, 0);
                break;
            case SHOW_CENTER:
                toast.setGravity(SHOW_CENTER, 0, 0);
                break;
            case SHOW_BOTTOM:
                toast.setGravity(SHOW_BOTTOM, 0, 0);
                break;
            default:
                toast.setGravity(SHOW_BOTTOM, 0, 80);
                break;
        }
        toast.setDuration(duration);
        toast.setView(toastView);
        toast.show();
    }
布局文件

不带图片的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll_toast_text_msg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:padding="10dp">

    <TextView
        android:id="@+id/tv_toast_text_msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:hint="提示"
        android:textColor="#000000"
        android:textSize="20sp"
        tools:ignore="HardcodedText,PxUsage" />

</LinearLayout>

带图片的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll_toast_img_msg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:padding="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        tools:ignore="UselessParent">

        <ImageView
            android:id="@+id/img_toast_img_msg_top"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@mipmap/icon_toast_tips"
            tools:ignore="ContentDescription" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:gravity="center"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/img_toast_img_msg_left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@mipmap/icon_toast_tips"
                tools:ignore="ContentDescription" />

            <TextView
                android:id="@+id/tv_toast_img_msg"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:gravity="center"
                android:hint="提示"
                android:textColor="#000000"
                android:textSize="18sp"
                tools:ignore="HardcodedText,PxUsage" />

            <ImageView
                android:id="@+id/img_toast_img_msg_right"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:src="@mipmap/icon_toast_tips"
                tools:ignore="ContentDescription" />

        </LinearLayout>


        <ImageView
            android:id="@+id/img_toast_img_msg_bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@mipmap/icon_toast_tips"
            tools:ignore="ContentDescription" />
    </LinearLayout>
</LinearLayout>

源码链接:https://github.com/JetQiao/everythingIsOK.git.
PS:代码在不断更新中…

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值