美文网首页
Toast工具类Toast统一管理类

Toast工具类Toast统一管理类

作者: 票务系统曾坪 | 来源:发表于2018-11-26 13:55 被阅读0次

    package jay.com.socket.util;

    import android.content.Context;

    import android.widget.Toast;

    /**

    * Toast统一管理类

    */

    public class ToastUtil {

        private static Toast toast;

        /**

        * 短时间显示Toast

        */

        public static void showShort(Context ct, CharSequence msg) {

            if (null == toast) {

                toast = Toast.makeText(ct, msg, Toast.LENGTH_SHORT);

                // toast.setGravity(Gravity.CENTER, 0, 0);

            } else {

                toast.setText(msg);

            }

            toast.show();

        }

        /**

        * 短时间显示Toast

        */

        public static void showShort(Context ct, int msg) {

            if (null == toast) {

                toast = Toast.makeText(ct, msg, Toast.LENGTH_SHORT);

                // toast.setGravity(Gravity.CENTER, 0, 0);

            } else {

                toast.setText(msg);

            }

            toast.show();

        }

        /**

        * 长时间显示Toast

        */

        public static void showLong(Context ct, CharSequence msg) {

            if (null == toast) {

                toast = Toast.makeText(ct, msg, Toast.LENGTH_LONG);

                // toast.setGravity(Gravity.CENTER, 0, 0);

            } else {

                toast.setText(msg);

            }

            toast.show();

        }

        /**

        * 长时间显示Toast

        */

        public static void showLong(Context ct, int msg) {

            if (null == toast) {

                toast = Toast.makeText(ct, msg, Toast.LENGTH_LONG);

                // toast.setGravity(Gravity.CENTER, 0, 0);

            } else {

                toast.setText(msg);

            }

            toast.show();

        }

        /**

        * 自定义显示Toast时间

        */

        public static void show(Context ct, CharSequence msg, int duration) {

            if (null == toast) {

                toast = Toast.makeText(ct, msg, duration);

                // toast.setGravity(Gravity.CENTER, 0, 0);

            } else {

                toast.setText(msg);

            }

            toast.show();

        }

        /**

        * 自定义显示Toast时间

        */

        public static void show(Context ct, int msg, int duration) {

            if (null == toast) {

                toast = Toast.makeText(ct, msg, duration);

                // toast.setGravity(Gravity.CENTER, 0, 0);

            } else {

                toast.setText(msg);

            }

            toast.show();

        }

        /**

        * Hide the toast, if any.

        */

        public static void hideToast() {

            if (null != toast) {

                toast.cancel();

            }

        }

    }

    相关文章

      网友评论

          本文标题:Toast工具类Toast统一管理类

          本文链接:https://www.haomeiwen.com/subject/klkbqqtx.html