android应用开发之多次触发toast的处理

【场景】:当一个比如listview多次点击toast,这时候会出现toast延迟的情况

【处理】:以最后一次toast为准处理

【使用方法】:

private ToastUtils mToastUtils;
mToastUtils = new ToastUtils(this);
mToastUtils.toast("toast");  //默认的持续的时间是0
mToastUtils.toast("toast",1);//可以自定义toast的持续时间


【实现如下】:


ToastUtils.java

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

public class ToastUtils {

	public ToastUtils(Context context) {
		ToastMsg.BUILDER.init(context);
	}

	public enum ToastMsg {
		BUILDER;

		private Toast toast;
		private TextView tv;
		private View view;

		private void init(Context context) {
			view = LayoutInflater.from(context).inflate(R.layout.toast, null);
			tv = (TextView) view.findViewById(R.id.tv_toast);
			toast = new Toast(context);
			toast.setView(view);
		}

		public void showToast(CharSequence text, int duration) {
			if (text.length() != 0) {
				tv.setText(text);
				toast.setDuration(duration);
				toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0,
						40);
				toast.show();
			}
		}

		public void showToast(int id, int duration) {
			if (id != 0) {
				tv.setText(id);
				toast.setDuration(duration);
				toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0,
						40);
				toast.show();
			}
		}
	}

	public void toast(String text) {
		ToastMsg.BUILDER.showToast(text, 0);
	}

	public void toast(String text, int duration) {
		ToastMsg.BUILDER.showToast(text, duration);
	}

	public void toast(int id) {
		ToastMsg.BUILDER.showToast(id, 0);
	}

}


toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/toast_frame"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv_toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="left|center"
        android:paddingLeft="3dp"
        android:paddingRight="3dp"
        android:shadowColor="@android:color/white"
        android:shadowDx="1.0"
        android:shadowDy="1.0"
        android:textColor="@android:color/white"
        android:textSize="14.0sp" />

</LinearLayout>

toast_frame.xml

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

    <corners android:radius="4dip" />

    <padding
        android:bottom="10dip"
        android:left="6dip"
        android:right="6dip"
        android:top="10dip" />

    <gradient
        android:endColor="#323336"
        android:startColor="#323336" />

    <stroke
        android:width="1dip"
        android:color="#ffffff" />

</shape>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值