ToastUtils
public class ToastUtils {
private static Toast toast;
/**
* 短時間顯示 Toast,位置居下
*
* @param msg 顯示字符串
*/
public static void showShortToast(String msg) {
if (getAppContext() != null) {
if (toast == null) {
toast = Toast.makeText(getAppContext(), msg, Toast.LENGTH_SHORT);
} else {
toast.setText(msg);
}
}
toast.show();
}
/**
* 短時間顯示 Toast,位置居中
*
* @param msg 顯示字符串
*/
public static void showShortToastCenter(String msg) {
if (getAppContext() != null) {
if (toast == null) {
toast = Toast.makeText(getAppContext(), msg, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
} else {
toast.setText(msg);
}
}
toast.show();
}
/**
* 短時間顯示Toast【居上】
*
* @param msg 顯示的內(nèi)容-字符串
*/
public static void showShortToastTop(String msg) {
if (MyAPP.getAppContext() != null) {
if (toast == null) {
toast = Toast.makeText(MyAPP.getAppContext(), msg, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 0, 0);
} else {
toast.setText(msg);
}
toast.show();
}
}
/**
* 長時間顯示Toast【居下】
*
* @param msg 顯示的內(nèi)容-字符串
*/
public static void showLongToast(String msg) {
if (MyAPP.getAppContext() != null) {
if (toast == null) {
toast = Toast.makeText(MyAPP.getAppContext(), msg, Toast.LENGTH_LONG);
} else {
toast.setText(msg);
}
toast.show();
}
}
/**
* 長時間顯示Toast【居中】
*
* @param msg 顯示的內(nèi)容-字符串
*/
public static void showLongToastCenter(String msg) {
if (MyAPP.getAppContext() != null) {
if (toast == null) {
toast = Toast.makeText(MyAPP.getAppContext(), msg, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
} else {
toast.setText(msg);
}
toast.show();
}
MyAPP
public class MyAPP extends Application {
private static Context AppContext;
@Override
public void onCreate() {
super.onCreate();
AppContext = getApplicationContext();
}
public static Context getAppContext(){
return AppContext;
}
}