您的位置:首页 > 其它

Toast 使用小技巧

2012-12-03 11:48 337 查看
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();

        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: