Back to project page CommonLibs.
The source code is released under:
Apache License
If you think the Android project CommonLibs listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.alex.common.utils; /* w ww . j av a2 s .c om*/ import android.content.Context; import android.widget.Toast; /** * Toast??? * @author caisenchuan */ public class ToastUtils { /*-------------------------- * ????? *-------------------------*/ /*-------------------------- * ??? *-------------------------*/ /*-------------------------- * ???????? *-------------------------*/ private static Toast mSingletonToast = null; /*-------------------------- * public?? *-------------------------*/ public static void initSingletonToast(Context context) { mSingletonToast = Toast.makeText(context, "", Toast.LENGTH_SHORT); } /** * ?????Toast * @param context * @param str ???????? * @param singleton true?????Toast????????Toast */ public static void showShortToast(Context context, String str, boolean singleton) { show(context, str, false, singleton); } /** * ?????Toast * @param context * @param id ??????id * @param singleton true?????Toast????????Toast */ public static void showShortToast(Context context, int id, boolean singleton) { show(context, context.getString(id), false, singleton); } /** * ?????Toast * @param context * @param str ???????? * @param singleton true?????Toast????????Toast */ public static void showLongToast(Context context, String str, boolean singleton) { show(context, str, true, singleton); } /** * ?????Toast * @param context * @param id ??????id * @param singleton true?????Toast????????Toast */ public static void showLongToast(Context context, int id, boolean singleton) { show(context, context.getString(id), true, singleton); } /*-------------------------- * protected??packet?? *-------------------------*/ /*-------------------------- * private?? *-------------------------*/ /** * ?????????????????????Toast * @param context ??? * @param text ??????? * @param lengthLong true????Toast?????????Toast * @param singleton true?????Toast????????Toast */ private static void show(Context context, String text, boolean lengthLong, boolean singleton) { int duration = Toast.LENGTH_SHORT; if(lengthLong) { duration = Toast.LENGTH_LONG; } if(singleton && mSingletonToast != null) { mSingletonToast.setText(text); mSingletonToast.setDuration(duration); mSingletonToast.show(); } else { Toast.makeText(context, text, duration).show(); } } }