Toast 使用小技巧

Toast 使用过程时,需要自定义显示布局,规避多次点击问题,自己常用的两种方法。

1.自定义一个Toast 工具类,实现自定义布局等。

public class ToastUtils {
    private static Handler handler = new Handler(Looper.getMainLooper());
    private static Toast toast = null;
    private static Object synObj = new Object();

    public static void showToast(final Context context, final String msg,boolean flag) {
        showToast(context, msg, Toast.LENGTH_SHORT,flag);
    }

    public static void showToast(final Context context, final int resId,boolean flag) {
        showToast(context, resId, Toast.LENGTH_SHORT,flag);
    }
    
    public static void showToast(final Context context, final String msg,
            final int len,final boolean flag) {
        new Thread(new Runnable() {
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        synchronized (synObj) {// 同步锁
                            if (toast != null) {
                                toast.cancel();
                                toast.setDuration(len);
                            } else {
                                toast = Toast.makeText(context, msg, len);
                                View layout = LayoutInflater.from(context)
                                        .inflate(R.layout.my_toast, null);// 自定义布局
                                toast.setView(layout);
                            }

                            //根据不同的情况显示不同的Toast信息
                            if(flag){    
                                ((TextView)toast.getView().findViewById(R.id.TextViewShortInfo)).setVisibility(4);
                                ((TextView)toast.getView().findViewById(R.id.TextViewLongInfo)).setVisibility(0);
                                ((TextView) toast.getView().findViewById(R.id.TextViewLongInfo)).setText(msg);
                            }else{
                                ((TextView)toast.getView().findViewById(R.id.TextViewLongInfo)).setVisibility(4);
                                ((TextView)toast.getView().findViewById(R.id.TextViewShortInfo)).setVisibility(0);
                                ((TextView) toast.getView().findViewById(R.id.TextViewShortInfo)).setText(msg);
                            }
                            toast.show();
                        }
                    }
                });
            }
        }).start();
    }

    public static void showToast(final Context context, final int resId,
            final int len,final boolean flag) {
        new Thread(new Runnable() {
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        synchronized (synObj) {// 同步锁
                            if(toast != null) {
                                toast.cancel();
                                toast.setDuration(len);
                            } else {
                                toast = Toast.makeText(context, resId, len);
                                View layout = LayoutInflater.from(context)
                                        .inflate(R.layout.my_toast, null);// 自定义布局
                                toast.setView(layout);
                            }
                            if(flag){
                                ((TextView)toast.getView().findViewById(R.id.TextViewShortInfo)).setVisibility(8);
                                ((TextView)toast.getView().findViewById(R.id.TextViewLongInfo)).setVisibility(0);
                                ((TextView) toast.getView().findViewById(R.id.TextViewLongInfo)).setText(resId);
                            }else{
                                ((TextView)toast.getView().findViewById(R.id.TextViewLongInfo)).setVisibility(8);
                                ((TextView)toast.getView().findViewById(R.id.TextViewShortInfo)).setVisibility(0);
                                ((TextView) toast.getView().findViewById(R.id.TextViewShortInfo)).setText(resId);
                            }
                            toast.show();
                        }
                    }
                });
            }
        }).start();
    }

}


2. 如果只是普通的规避多次点击的需求,可以这样处理。

    private void showToast(String msg)
    {
        if(mToast == null)
        {
            mToast = Toast.makeText(this,msg, 3);
            mToast.show();
        }
        else
        {
            mToast.cancel();
            mToast.setText(msg);
            mToast.show();
        }
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值