Dialog
dialog尽量使用 android.support.v7.app.AlertDialog 包下的,外形更符合现在的审美观。
Toast
为了避免弹出多个toast,可以采用下面的工具类
public class ToastUtil {
private static Context context;
private static Toast toast = null;
private static String oldMsg;
private static long oneTime = 0;
private static long twoTime = 0;
public static Context getContext() {
return context;
}
/**
* Application中初始化
* public class App extends Application {
*
* @param context
* @Override public void onCreate() {
* super.onCreate();
* ToastUtil.setContext(this);
* }
* }
*/
public static void setContext(Context context) {
ToastUtil.context = context;
}
/**
* 使吐司只更新内容和时间,而不再是重新弹出
*
* @param text 内容
*/
public static void toast(String text) {
if (toast == null) {
toast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
toast.show();
oneTime = System.currentTimeMillis();
} else {
twoTime = System.currentTimeMillis();
if (text.equals(oldMsg)) {
if (twoTime - oneTime > Toast.LENGTH_SHORT) {
toast.show();
}
} else {
oldMsg = text;
toast.setText(text);
toast.show();
}
}
oneTime = twoTime;
}
}
Snackbar
使用时先添加 com.android.support:design 库
默认底部弹出,在有底部导航栏的手机上会和导航栏重合,解决方法是把第一个view参数设为:
View view = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);