自定义Toast(解决重复显示及控制显示时间)

Android显示Toast,点击的多了,会很难看,所以,做一个有新的Toast要显示的话,就把旧的撤销掉。
可自定义Toast样式。

Toast工具类:

public class ToastUtil {

    private static List<Toast> toastList = new ArrayList();

    //显示文本的Toast
    public static void showTextToast(Context context, String message){
        cancelAll();
        View toastView= LayoutInflater.from(context).inflate(R.layout.toast_text_layout,null);
        TextView text = (TextView) toastView.findViewById(R.id.tv_message);
        text.setText(message);
        Toast toast=new Toast(context);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(toastView);
        toastList.add(toast);
        toast.show();
    }

    public static void cancelAll() {
        if (!toastList.isEmpty()){
            for (Toast toast : toastList) {
                toast.cancel();
            }
            toastList.clear();
        }
    }
}

思想就是:把老的加入到一个list里边,每有新的要显示,就把旧的先撤销,在显示新的。

toast_text_layout如下:

<?xml version="1.0" encoding="utf-8"?>
<Layout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/toast_background"
    android:gravity="center">
    <TextView
        android:id="@+id/tv_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:text="Toast"
        android:paddingTop="30px"
        android:paddingLeft="30px"
        android:paddingRight="30px"
        android:paddingBottom="30px"
        android:textSize="40px"
        />
</LinearLayout>

背景toast_background:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <solid android:color="#80000000" />
    <corners
        android:topLeftRadius="8dip"
        android:topRightRadius="8dip"
        android:bottomLeftRadius="8dip"
        android:bottomRightRadius="8dip"
        >
    </corners>
</shape>

调用的话:

ToastUtil.showTextToast(MainActivity.this,"弹出了一个吐司");

现在又有一个新的需求,就是设置吐司的时间,自定义吐司的时间,想法就是加一个定时器来控制显示和消失:
所以将自定义的Toast类改成了:

public class ToastUtil {

    private static List<Toast> toastList = new ArrayList();

    //显示文本的Toast
    public static void showTextToast(Context context, String message,int num){
        cancelAll();
        View toastView= LayoutInflater.from(context).inflate(R.layout.toast_text_layout,null);
        TextView text = (TextView) toastView.findViewById(R.id.tv_message);
        text.setText(message);
        Toast toast=new Toast(context);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(toastView);
        toastList.add(toast);
        //toast.show();
        showMyToast(toast,num);
    }

    public static void cancelAll() {
        if (!toastList.isEmpty()){
            for (Toast toast : toastList) {
                toast.cancel();
            }
            toastList.clear();
        }
    }

    public static void showMyToast(final Toast toast, final int countNum) {
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                toast.show();
            }
        }, 0);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                toast.cancel();
                timer.cancel();
            }
        }, countNum);

    }
}

这样就加了一个时间的限定来控制每一个吐司显示的时间,布局什么的也都没有变化,调用的时候,多传一个表示时间的参数就行了,例:

 ToastUtil.showTextToast(context,bean.getMessage(),2500);

这条吐司的显示时间为2.5秒。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值