其他分享
首页 > 其他分享> > Android SnackBar

Android SnackBar

作者:互联网

 

 

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

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片  
  1. public class MainActivity extends Activity {  
  2.     private SnackBar mSnackBar;  
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.         mSnackBar = new SnackBar.Builder(this)  
  8.         .withMessage("提示信息")  
  9.         .withDuration(SnackBar.LONG_SNACK)  
  10.         .show();  
  11.     }  
  12.   
  13. }  

 

下载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中,使用:

 

 

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    boolean flag = true;
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        return mDoubleClickExitHelper.onKeyDown(keyCode, navigationView);
}
    return flag;
}


 

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

标签:return,SnackBar,private,snackbar,import,Android,public,android
来源: https://www.cnblogs.com/skiwnchhw/p/10349230.html