首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有可能在不采取任何措施的情况下自动关闭安卓快餐栏(LENGTH_INDEFINITE)?

有没有可能在不采取任何措施的情况下自动关闭安卓快餐栏(LENGTH_INDEFINITE)?
EN

Stack Overflow用户
提问于 2016-01-31 14:13:00
回答 4查看 7.6K关注 0票数 3

我已经展示了android小吃店无限长度但如何关闭小吃店没有任何行动或持续时间,而互联网连接。我必须检查互联网连接或not.after互联网连接的小吃店将自动关闭而不采取任何行动或duration.If任何人知道好心帮助我。

以下是我的代码

代码语言:javascript
复制
public static void snack (HashMap actions,int priority,String message,Activity context){
    Snackbar B = Snackbar.make(context.findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG);
    if(actions!=null){
    Iterator iterator = actions.entrySet().iterator();
        B.setDuration(Snackbar.LENGTH_INDEFINITE);
    while (iterator.hasNext()) {
        Map.Entry pair = (Map.Entry)iterator.next();
        B.setAction((String)pair.getKey(),(View.OnClickListener)pair.getValue());
        iterator.remove(); // avoids a ConcurrentModificationException
    }}
    switch (priority)
    {
        case 0:
            B.getView().setBackgroundColor(context.getResources().getColor(R.color.color_pinkbutton));
            break;
        case 1:
            B.getView().setBackgroundColor(Color.parseColor("#66ccff"));
            break;
        case 2:
            B.getView().setBackgroundColor(Color.parseColor("#66ff33"));
            break;
    }
    B.show();

在调用上面提到的方法之后,使用activity的方法如下所示

代码语言:javascript
复制
If (NetworkCheck.isNetworkAvailable(this) == false) {
    MyApplication.snack(null, 0, "Network Connection failed.",class.this);
else
EN

回答 4

Stack Overflow用户

发布于 2016-09-07 23:53:42

我创建了这个单例实用类。它保持了应用程序类的整洁,并为小吃店未来的可维护性做了最好的准备。

代码语言:javascript
复制
public class SnackBarUtils {
    private static SnackBarUtils mInstance = null;
    private Snackbar mSnackBar;

    private SnackBarUtils() {

    }

    public static SnackBarUtils getInstance() {
        if (mInstance == null) {
            mInstance = new SnackBarUtils();
        }
        return mInstance;
    }

    public void hideSnackBar() {
        if (mSnackBar != null) {
            mSnackBar.dismiss();
        }
    }

    public void showProblemSnackBar(final Activity activity, final String message) {
        mSnackBar = Snackbar.make(activity.findViewById(android.R.id.content), message, Snackbar.LENGTH_INDEFINITE);
        // Changing action button text color
        View sbView = mSnackBar.getView();
        TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
        textView.setTextColor(Color.YELLOW);
        mSnackBar.show();
    }
}
票数 9
EN

Stack Overflow用户

发布于 2016-01-31 14:51:02

您可以按如下方式在代码中进行更改:

代码语言:javascript
复制
Snackbar B; //make it as global

    public static void snack (HashMap actions,int priority,String message,Activity context){
      B = Snackbar.make(context.findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG);
      if(actions!=null){
      Iterator iterator = actions.entrySet().iterator();
        B.setDuration(Snackbar.LENGTH_INDEFINITE);
      while (iterator.hasNext()) {
        Map.Entry pair = (Map.Entry)iterator.next();
        B.setAction((String)pair.getKey(),(View.OnClickListener)pair.getValue());
        iterator.remove(); // avoids a ConcurrentModificationException
      }}
      switch (priority){
        case 0:
                B.getView().setBackgroundColor(context.getResources().getColor(R.color.color_pinkbutton));
            break;
        case 1:
            B.getView().setBackgroundColor(Color.parseColor("#66ccff"));
            break;
        case 2:
            B.getView().setBackgroundColor(Color.parseColor("#66ff33"));
            break;
    }
    B.show();

使用另一种方法将snackbar隐藏为

代码语言:javascript
复制
private static hideSnackbar(){
   if(B !=null && B.isShown()){
           B.dismiss();
   }
}

在你的情况下

代码语言:javascript
复制
If (NetworkCheck.isNetworkAvailable(this) == false) {
    MyApplication.snack(null, 0, "Network Connection failed.",class.this)
}else{
    MyApplication.hideSnackbar();
}
票数 1
EN

Stack Overflow用户

发布于 2016-01-31 14:56:00

您只需创建广播接收器,并在您的活动/片段中接收以下事件并订阅它

完整信息在此处http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35110955

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档