展示的效果就这样简单就看你是有那个需求了。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>