zoukankan      html  css  js  c++  java
  • Android SnackBar

    展示的效果就这样简单就看你是有那个需求了。SnackBar的创建过程使用了Builder设计模式,有不同需求的可以更具需求结合代码创建出需要的效果。

    public class MainActivity extends Activity {  
        private SnackBar mSnackBar;  
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);  
            mSnackBar = new SnackBar.Builder(this)  
            .withMessage("提示信息")  
            .withDuration(SnackBar.LONG_SNACK)  
            .show();  
        }  
      
    }  

    下载demo

    分享个退出提示工具类DoubleClickExitHelper.java
    package miuyongjun.twentysix.utils;
    
    
    import android.app.Activity;
    import android.os.Handler;
    import android.os.Looper;
    import android.support.design.widget.Snackbar;
    import android.view.KeyEvent;
    import android.view.View;
    
    /**
     * 双击退出工具类
     *
     * @author wxc
     */
    public class DoubleClickExitHelper {
    
        private final Activity mActivity;
    
        private boolean isOnKeyBacking;
        private Handler mHandler;
        private Snackbar snackbar;
    
        public DoubleClickExitHelper(Activity activity) {
            mActivity = activity;
            mHandler = new Handler(Looper.getMainLooper());
            AppManager.getAppManager().addActivity(mActivity);
        }
    
        /**
         * Activity onKeyDown事件
         */
        public boolean onKeyDown(int keyCode, View view) {
            if (keyCode != KeyEvent.KEYCODE_BACK) {
                return false;
            }
            if (isOnKeyBacking) {
                mHandler.removeCallbacks(onBackTimeRunnable);
                if (snackbar != null) {
                    snackbar.dismiss();
                }
                AppManager.getAppManager().AppExit(mActivity);
                return true;
            } else {
                isOnKeyBacking = true;
                if (snackbar == null) {
                    snackbar = Snackbar.make(view, "再次点击退出应用", 2000);
                }
                snackbar.show();
                mHandler.postDelayed(onBackTimeRunnable, 2000);
                return true;
            }
        }
    
        private Runnable onBackTimeRunnable = new Runnable() {
            @Override
            public void run() {
                isOnKeyBacking = false;
                if (snackbar != null) {
                    snackbar.dismiss();
                }
            }
        };
    }

    在MainActivity中,使用:

    <pre code_snippet_id="1703141" snippet_file_name="blog_20160531_2_7463231" style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Source Code Pro';font-size:10.5pt;"><span style="color:#bbb529;">@Override
    </span><span style="color:#cc7832;">public boolean </span><span style="color:#ffc66d;">onKeyDown</span>(<span style="color:#cc7832;">int </span>keyCode<span style="color:#cc7832;">, </span>KeyEvent event) {
        <span style="color:#cc7832;">boolean </span><span style="color:#68b5ee;">flag </span>= <span style="color:#cc7832;">true;
    </span><span style="color:#cc7832;">    if </span>(keyCode == KeyEvent.<span style="color:#9876aa;"><em>KEYCODE_BACK</em></span>) {
            <span style="color:#cc7832;">return </span><span style="color:#9876aa;">mDoubleClickExitHelper</span>.onKeyDown(keyCode<span style="color:#cc7832;">, </span><span style="color:#9876aa;">navigationView</span>)<span style="color:#cc7832;">;
    </span><span style="color:#cc7832;">    </span>}
        <span style="color:#cc7832;">return </span><span style="color:#68b5ee;">flag</span><span style="color:#cc7832;">;
    </span>}</pre><br>
  • 相关阅读:
    wireshark tcp 协议分析
    Wireshark基本介绍和学习TCP三次握手
    利用百度API(JavaScript 版)实现在地图上绘制任一多边形,并判断给定经纬度是否在多边形范围内。以及两点间的测距功能
    如何从零开始做代码评审
    Git版本控制:Github的使用之 多人协作及参与项目
    如何查看与分析IIS服务器日志?
    解决IIS无响应假死状态
    百度地图上的标注物太多导致界面卡顿的解决办法
    [转]搭建MySQL高可用负载均衡集群
    原型链相关
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5786212.html
Copyright © 2011-2022 走看看