全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

Android自定义单例AlertDialog详解

当Android开发处理错误信息时,经常会以Dialog的形式显示错误信息,但是每次都new一个Dialog,很麻烦,也增加程序的开销,所以今天就分享一种自定义单例AlertDialog

public class AlertDialog {
  private static AlertDialog alertDialog = null;
  private Context context;
  private Dialog dialog;
  private LinearLayout lLayout_bg;
  private TextView txt_title;
  private TextView txt_msg;
  private Button btn_neg;
  private Button btn_pos;
  private ImageView img_line;
  private Display display;
  private boolean showTitle = false;
  private boolean showMsg = false;
  private boolean showPosBtn = false;
  private boolean showNegBtn = false;

  public static AlertDialog getInstance(Context context){
    if (alertDialog==null){
      synchronized (AlertDialog.class) {
        if (alertDialog == null) {
          alertDialog = new AlertDialog(context).builder();
        }
      }
    }
    return alertDialog;
  }
  public AlertDialog(Context context) {
    this.context = context;
    WindowManager windowManager = (WindowManager) context
        .getSystemService(Context.WINDOW_SERVICE);
    display = windowManager.getDefaultDisplay();
  }

  public AlertDialog builder() {
    // 获取Dialog布局
    View view = LayoutInflater.from(context).inflate(R.layout.view_alertdialog, null);

    // 获取自定义Dialog布局中的控件
    lLayout_bg = (LinearLayout) view.findViewById(R.id.lLayout_bg);
    txt_title = (TextView) view.findViewById(R.id.txt_title);
    txt_title.setVisibility(View.GONE);
    txt_msg = (TextView) view.findViewById(R.id.txt_msg);
    txt_msg.setVisibility(View.GONE);
    btn_neg = (Button) view.findViewById(R.id.btn_neg);
    btn_neg.setVisibility(View.GONE);
    btn_pos = (Button) view.findViewById(R.id.btn_pos);
    btn_pos.setVisibility(View.GONE);
    img_line = (ImageView) view.findViewById(R.id.img_line);
    img_line.setVisibility(View.GONE);

    // 定义Dialog布局和参数
    dialog = new Dialog(context, R.style.AlertDialogStyle);
    dialog.setContentView(view);

    // 调整dialog背景大小
    lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
        .getWidth() * 0.85), LayoutParams.WRAP_CONTENT));

    return this;
  }

  public AlertDialog setTitle(String title) {
    showTitle = true;
    if ("".equals(title)) {
      txt_title.setText("标题");
    } else {
      txt_title.setText(title);
    }
    return this;
  }

  public AlertDialog setMsg(String msg) {
    showMsg = true;
    if ("".equals(msg)) {
      txt_msg.setText("内容");
    } else {
      txt_msg.setText(msg);
    }
    return this;
  }
  public AlertDialog setMsg(int rId) {
    showMsg = true;
    txt_msg.setText(rId);
    return this;
  }

  public AlertDialog setCancelable(boolean cancel) {
    dialog.setCancelable(cancel);
    return this;
  }

  public AlertDialog setPositiveButton(String text,
      final OnClickListener listener) {
    showPosBtn = true;
    if ("".equals(text)) {
      btn_pos.setText("确定");
    } else {
      btn_pos.setText(text);
    }
    btn_pos.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if(listener!=null) {
          listener.onClick(v);
        }
        dialog.dismiss();
      }
    });
    return this;
  }

  public AlertDialog setNegativeButton(String text,
      final OnClickListener listener) {
    showNegBtn = true;
    if ("".equals(text)) {
      btn_neg.setText("取消");
    } else {
      btn_neg.setText(text);
    }
    btn_neg.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if(listener!=null) {
          listener.onClick(v);
        }
        dialog.dismiss();
      }
    });
    return this;
  }

  private void setLayout() {
    if (!showTitle && !showMsg) {
      txt_title.setText("");
      txt_title.setVisibility(View.VISIBLE);
    }

    if (showTitle) {
      txt_title.setVisibility(View.VISIBLE);
    }

    if (showMsg) {
      txt_msg.setVisibility(View.VISIBLE);
    }

    if (!showPosBtn && !showNegBtn) {
      btn_pos.setText("确定");
      btn_pos.setVisibility(View.VISIBLE);
      btn_pos.setBackgroundResource(R.drawable.alertdialog_single_selector);
      btn_pos.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          dialog.dismiss();
        }
      });
    }

    if (showPosBtn && showNegBtn) {
      btn_pos.setVisibility(View.VISIBLE);
      btn_pos.setBackgroundResource(R.drawable.alertdialog_right_selector);
      btn_neg.setVisibility(View.VISIBLE);
      btn_neg.setBackgroundResource(R.drawable.alertdialog_left_selector);
      img_line.setVisibility(View.VISIBLE);
    }

    if (showPosBtn && !showNegBtn) {
      btn_pos.setVisibility(View.VISIBLE);
      btn_pos.setBackgroundResource(R.drawable.alertdialog_single_selector);
    }

    if (!showPosBtn && showNegBtn) {
      btn_neg.setVisibility(View.VISIBLE);
      btn_neg.setBackgroundResource(R.drawable.alertdialog_single_selector);
    }
  }

  public void show() {
    setLayout();
    dialog.show();
  }
}

布局文件view_alertdialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/lLayout_bg"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="@drawable/alert_bg"
  android:orientation="vertical">

  <TextView
    android:id="@+id/txt_title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="15dp"
    android:gravity="center"
    android:text="提示"
    android:textColor="@color/black"
    android:textSize="18dp" />


  <TextView
    android:id="@+id/txt_msg"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:gravity="center"
    android:text="您确定要退出吗?"
    android:textColor="@color/black"
    android:textSize="16dp" />

  <ImageView
    android:layout_width="match_parent"
    android:layout_height="0.5dp"
    android:layout_marginTop="10dp"
    android:background="@color/alertdialog_line" />

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
      android:id="@+id/btn_neg"
      android:layout_width="wrap_content"
      android:layout_height="43dp"
      android:layout_weight="1"
      android:background="@drawable/alertdialog_left_selector"
      android:gravity="center"
      android:text="确定"
      android:textColor="@color/bigtextcolor"
      android:textSize="16sp" />

    <ImageView
      android:id="@+id/img_line"
      android:layout_width="0.5dp"
      android:layout_height="43dp"
      android:background="@color/alertdialog_line" />

    <Button
      android:id="@+id/btn_pos"
      android:layout_width="wrap_content"
      android:layout_height="43dp"
      android:layout_weight="1"
      android:background="@drawable/alertdialog_right_selector"
      android:gravity="center"
      android:text="取消"
      android:textColor="@color/themecolor"
      android:textSize="16sp" />
  </LinearLayout>

</LinearLayout>

效果显示

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


# Android  # 单例  # AlertDialog  # Android AlertDialog的几种用法详解  # Android中AlertDialog四种对话框的最科学编写用法(实例代码)  # Android去除AlertDialog的按钮栏的分隔线  # Android编程自定义AlertDialog样式的方法详解  # Android使用AlertDialog创建对话框  # Android开发实现AlertDialog中View的控件设置监听功能分析  # Android 自定义AlertDialog对话框样式  # Android AlertDialog多种创建方式案例详解  # 自定义  # 错误信息  # 每次都  # 会以  # 大家多多  # 很麻烦  # View 


相关文章: 存储型VPS适合搭建中小型网站吗?  广平建站公司哪家专业可靠?如何选择?  厦门模型网站设计制作公司,厦门航空飞机模型掉色怎么办?  css网站制作参考文献有哪些,易聊怎么注册?  建站之星如何一键生成手机站?  武汉网站制作费用多少,在武汉武昌,建面100平方左右的房子,想装暖气片,费用大概是多少啊?  云南网站制作公司有哪些,云南最好的招聘网站是哪个?  如何高效完成独享虚拟主机建站?  如何通过主机屋免费建站教程十分钟搭建网站?  建站为何优先选择香港服务器?  如何自定义建站之星网站的导航菜单样式?  建站之星如何实现网站加密操作?  如何设计高效校园网站?  建站之星安装提示数据库无法连接如何解决?  建站VPS配置与SEO优化指南:关键词排名提升策略  制作农业网站的软件,比较好的农业网站推荐一下?  建站主机选购指南:核心配置与性价比推荐解析  山东网站制作公司有哪些,山东大源集团官网?  建站之星后台搭建步骤解析:模板选择与产品管理实操指南  如何通过免费商城建站系统源码自定义网站主题与功能?  如何撰写建站申请书?关键要点有哪些?  定制建站流程解析:需求评估与SEO优化功能开发指南  如何用PHP快速搭建高效网站?分步指南  如何通过商城免费建站系统源码自定义网站主题?  建站主机如何选?性能与价格怎样平衡?  如何用手机制作网站和网页,手机移动端的网站能制作成中英双语的吗?  大型企业网站制作流程,做网站需要注册公司吗?  宝塔建站无法访问?如何排查配置与端口问题?  ,石家庄四十八中学官网?  建站之星安装需要哪些步骤及注意事项?  如何挑选最适合建站的高性能VPS主机?  如何在阿里云部署织梦网站?  如何快速搭建虚拟主机网站?新手必看指南  自助网站制作软件,个人如何自助建网站?  如何通过西部数码建站助手快速创建专业网站?  如何用低价快速搭建高质量网站?  定制建站是什么?如何实现个性化需求?  深圳网站制作公司好吗,在深圳找工作哪个网站最好啊?  如何选择网络建站服务器?高效建站必看指南  香港服务器租用费用高吗?如何避免常见误区?  高端建站如何打造兼具美学与转化的品牌官网?  免费网站制作appp,免费制作app哪个平台好?  制作企业网站建设方案,怎样建设一个公司网站?  常州自助建站:操作简便模板丰富,企业个人快速搭建网站  小型网站建站如何选择虚拟主机?  阿里云网站搭建费用解析:服务器价格与建站成本优化指南  网站代码制作软件有哪些,如何生成自己网站的代码?  详解jQuery停止动画——stop()方法的使用  如何用AWS免费套餐快速搭建高效网站?  如何在Mac上搭建Golang开发环境_使用Homebrew安装和管理Go版本 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。