對安卓(Android)吐司的簡單封裝,ToastUtils工具類

在Android開發過程中,Toast大家一定不會陌生,但是每次使用Toast都要 Toast.makeText ... 讓人感到不爽,所以為了阻止懶病發作,簡單封裝了一個Toast的工具類,希望能幫到剛入坑的Android菜鳥,大神請繞路...!

開Android開發過程中,Toast大家一定不會陌生,但是每次使用Toast都要 Toast.makeText ... 讓人感到不爽,所以為了阻止懶病發作,簡單封裝了一個Toast的工具類,希望能幫到剛入坑的Android菜鳥,大神請繞路...!

<code>/** * ToastUtils工具類 * * @author CharlesRich * @email [email protected] * @mobile 18602438878 * @create 2020-02-10 00:13 */public class ToastUtils {    /** 之前顯示的內容 */    private static String mStr_Old_Message ;    /** Toast對象 */    private static Toast mToast = null ;    /** 第一次時間 */    private static long mLng_One_Time = 0 ;    /** 第二次時間 */    private static long mLng_Two_Time = 0 ;    /**     * 顯示短Toast     * */    public static void showShortToast(Context context,String text){        if (TextUtils.isEmpty(text)){            return;        }        Toast.makeText(context,text,Toast.LENGTH_SHORT).show();    }    public static void showShortToast(String text){        showShortToast(BaseApplication.getInstance(),text);    }    /**     * 顯示長Toast     *     * */    public static void showLongToast(Context context,String text){        if (TextUtils.isEmpty(text)){            return;        }        Toast.makeText(context,text,Toast.LENGTH_LONG).show();    }    public static void showLongToast(String text){        showLongToast(BaseApplication.getInstance(),text);    }    /**     * 顯示Toast,避免重複顯示Toast     * @param context     * @param text     */    public static void showToast(Context context,String text){        if (TextUtils.isEmpty(text)){            return;        }        if(mToast == null){            mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);            mToast.show() ;            mLng_One_Time = System.currentTimeMillis() ;        }else{            mLng_Two_Time = System.currentTimeMillis() ;            if(text.equals(mStr_Old_Message)){                if(mLng_Two_Time - mLng_One_Time > Toast.LENGTH_SHORT){                    mToast.show() ;                }            }else{                mStr_Old_Message = text ;                mToast.setText(text) ;                mToast.show() ;            }        }        mLng_One_Time = mLng_Two_Time ;    }}/<code>


分享到:


相關文章: