Android dialog,toast,snackbar小节

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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值