Android常常用到Toast,但是沒錯要寫很多,我隻想寫一些要展示的資訊,其他的懶得寫。
**
* Created by Kevin on //
* Blog:http://blog.csdn.net/student9128
* Description: the utils for toast.
*/
public class ToastUtils {
private static Toast mShortToast;
private static Toast mLongToast;
public static void showToast(Context context, String message) {
if (mShortToast == null) {
mShortToast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
}
mShortToast.setText(message);
mShortToast.show();
}
public static void showToast(String message, Context context) {
if (mShortToast == null) {
mShortToast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
}
mShortToast.setText(message);
mShortToast.show();
}
public static void showLongToast(Context context, String message) {
if (mLongToast == null) {
mLongToast = Toast.makeText(context, message, Toast.LENGTH_LONG);
}
mLongToast.setText(message);
mLongToast.show();
}
public static void showLongToast(String message, Context context) {
if (mLongToast == null) {
mLongToast = Toast.makeText(context, message, Toast.LENGTH_LONG);
}
mLongToast.setText(message);
mLongToast.show();
}
}
但是這樣沒錯還要寫context,我也很煩。于是就寫到了Base裡面。
public void showToast(String message) {
ToastUtils.showToast(this, message);
}
public void showLongToast(String message) {
ToastUtils.showLongToast(this, message);
}