Weavey's Blog

直接拿去用!几种常见Dialog2.0

前言

前面封装了项目中经常使用到的几种Dialog,自己在使用过程中也发现了一些问题,使用不是很方便,决定改动个别方法,故升到2.0。

引用方式

gradle引用

compile 'com.lai.weavey:dialog:2.0.1'

Maven配置

<dependency>
 <groupId>com.lai.weavey</groupId>
 <artifactId>dialog</artifactId>
 <version>2.0.1</version>
 <type>pom</type>
</dependency>

GitHub项目地址

https://github.com/weavey/NormalSelectDialog,欢迎fork或star,提出宝贵意见,有其他常用的Dialog可以告知,有时间将一起封装进本库。

2.0改动

  • Dialog的属性设置与1.3版本基本无差别,改动了点击事件的接口回调,统一管理并公用,将Dialog对象返回:
public interface DialogInterface {

interface OnLeftAndRightClickListener<T> {

void clickLeftButton(T dialog, View view);

void clickRightButton(T dialog, View view);
}

interface OnSingleClickListener<T> {

    void clickSingleButton(T dialog, View view);
}

interface OnItemClickListener<T> {

    void onItemClick(T dialog, View button, int position);
}

}
  • 将Dialog对象返回,一些比较不常用的属性可通过getDialog()方法获取Dialog后自行设置,更加灵活。例:

    NormalAlertDialog dialog = new NormalAlertDialog.Builder(MainActivity.this).setHeight(0.23f)  //屏幕高度*0.23
            .setTitleVisible(true)
            .setTitleText("温馨提示")
            .setContentText("是否关闭对话框?")
            .setSingleMode(true)
            .setSingleButtonText("关闭")
            .setCanceledOnTouchOutside(false)
            .setSingleListener(new DialogInterface.OnSingleClickListener<NormalAlertDialog>() {
                @Override
                public void clickSingleButton(NormalAlertDialog dialog, View view) {
                    dialog.dismiss();
                }
            })
            .build();
    
    //屏蔽返回键取消对话框
    dialog.getDialog().setOnKeyListener(new android.content.DialogInterface.OnKeyListener() {
    
        @Override
        public boolean onKey(android.content.DialogInterface dialog, int keyCode, KeyEvent
                event) {
            if(keyCode==KeyEvent.KEYCODE_BACK){
    
                return true;
            }
    
            return false;
        }
    });
    dialog.show();
    

使用方法

样式一:NormalSelectionDialog

NormalSelectionDialog

使用如下代码:

 ArrayList<String> s = new ArrayList<>();
s.add("Weavey0");
s.add("Weavey1");
s.add("Weavey2");
s.add("Weavey3");

new NormalSelectionDialog.Builder(this).setlTitleVisible(true)   //设置是否显示标题
        .setTitleHeight(65)   //设置标题高度
        .setTitleText("please select")  //设置标题提示文本
        .setTitleTextSize(14) //设置标题字体大小 sp
        .setTitleTextColor(R.color.colorPrimary) //设置标题文本颜色
        .setItemHeight(40)  //设置item的高度
        .setItemWidth(0.9f)  //屏幕宽度*0.9
        .setItemTextColor(R.color.colorPrimaryDark)  //设置item字体颜色
        .setItemTextSize(14)  //设置item字体大小
        .setCancleButtonText("Cancle")  //设置最底部“取消”按钮文本
        .setOnItemListener(new DialogInterface.OnItemClickListener<NormalSelectionDialog>() {

            @Override
            public void onItemClick(NormalSelectionDialog dialog, View button, int
                    position) {

                dialog.dismiss();
            }
        })
        .setCanceledOnTouchOutside(true)  //设置是否可点击其他地方取消dialog
        .build()
        .setDatas(s)
        .show();

样式二:NormalAlertDialog

NormalAlertDialog

使用如下代码:

new NormalAlertDialog.Builder(MainActivity.this).setTitleVisible(false)
            .setTitleText("温馨提示")
            .setTitleTextColor(R.color.black_light)
            .setContentText("是否关闭对话框?")
            .setContentTextColor(R.color.black_light)
            .setLeftButtonText("关闭")
            .setLeftButtonTextColor(R.color.gray)
            .setRightButtonText("不关闭")
            .setRightButtonTextColor(R.color.black_light)
            .setOnclickListener(new DialogInterface.OnLeftAndRightClickListener<NormalAlertDialog>() {
                @Override
                public void clickLeftButton(NormalAlertDialog dialog, View view) {

                    dialog.dismiss();
                }

                @Override
                public void clickRightButton(NormalAlertDialog dialog, View view) {

                    dialog.dismiss();
                }
            })
            .build()
            .show();

样式三:NormalAlertDialog(单键)

NormalAlertDialog

使用如下代码:

new NormalAlertDialog.Builder(MainActivity.this).setHeight(0.23f)  //屏幕高度*0.23
            .setWidth(0.65f)  //屏幕宽度*0.65
            .setTitleVisible(true).setTitleText("温馨提示")
            .setTitleTextColor(R.color.colorPrimary)
            .setContentText("是否关闭对话框?")
            .setContentTextColor(R.color.colorPrimaryDark)
            .setSingleMode(true).setSingleButtonText("关闭")
            .setSingleButtonTextColor(R.color.colorAccent)
            .setCanceledOnTouchOutside(true)
            .setSingleListener(new DialogInterface.OnSingleClickListener<NormalAlertDialog>() {
                @Override
                public void clickSingleButton(NormalAlertDialog dialog, View view) {
                    dialog.dismiss();
                }
            })
            .build()
            .show();

样式四:MDAlertDialog

MDAlertDialog

使用如下代码:

new MDAlertDialog.Builder(MainActivity.this)
          .setHeight(0.21f)  //屏幕高度*0.21
          .setWidth(0.7f)  //屏幕宽度*0.7
          .setTitleVisible(true)
          .setTitleText("温馨提示")
          .setTitleTextColor(R.color.black_light)
          .setContentText("确定发送文件?")
          .setContentTextColor(R.color.black_light)
          .setLeftButtonText("不发送")
          .setLeftButtonTextColor(R.color.gray)
          .setRightButtonText("发送")
          .setRightButtonTextColor(R.color.black_light)
          .setTitleTextSize(16)
          .setContentTextSize(14)
          .setButtonTextSize(14)
          .setOnclickListener(new DialogInterface.OnLeftAndRightClickListener<MDAlertDialog>() {

              @Override
              public void clickLeftButton(MDAlertDialog dialog, View view) {
                  dialog.dismiss();
              }

              @Override
              public void clickRightButton(MDAlertDialog dialog, View view) {
                  dialog.dismiss();
              }
          })
          .build()
          .show();

样式五:MDSelectionDialog

MDSelectionDialog

使用如下代码:

datas = new ArrayList<>();
datas.add("标为未读");
datas.add("置顶聊天");
datas.add("删除该聊天");

new MDSelectionDialog.Builder(MainActivity.this)
        .setCanceledOnTouchOutside(true)
        .setItemTextColor(R.color.black_light)
        .setItemHeight(50)
        .setItemWidth(0.8f)  //屏幕宽度*0.8
        .setItemTextSize(15)
        .setCanceledOnTouchOutside(true)
        .setOnItemListener(new DialogInterface.OnItemClickListener<MDSelectionDialog>() {
            @Override
            public void onItemClick(MDSelectionDialog dialog, View button, int position) {

                dialog.dismiss();
            }
        })
        .build()
        .setDatas(datas)
        .show();

样式六:MDEditDialog

MDEditDialog

使用如下代码:

new MDEditDialog.Builder(MainActivity.this).setTitleVisible(true)
            .setTitleText("修改用户名")
            .setTitleTextSize(20)
            .setTitleTextColor(R.color.black_light)
            .setContentText("Weavey")
            .setContentTextSize(18)
            .setMaxLength(7)
            .setHintText("7位字符")
            .setMaxLines(1)
            .setContentTextColor(R.color.colorPrimary)
            .setButtonTextSize(14)
            .setLeftButtonTextColor(R.color.colorPrimary)
            .setLeftButtonText("取消")
            .setRightButtonTextColor(R.color.colorPrimary)
            .setRightButtonText("确定")
            .setLineColor(R.color.colorPrimary)
            .setInputTpye(InputType.TYPE_CLASS_NUMBER)
            .setOnclickListener(new DialogInterface.OnLeftAndRightClickListener<MDEditDialog>
                    () {

                @Override
                public void clickLeftButton(MDEditDialog dialog, View view) {

                    dialog.getEditTextContent();
                    dialog.dismiss();
                }

                @Override
                public void clickRightButton(MDEditDialog dialog, View view) {

                    dialog.getEditTextContent();
                    dialog.dismiss();
                }
            })
            .setMinHeight(0.3f)
            .setWidth(0.8f)
            .build()
            .show();
Weavey wechat
欢迎扫一扫上面的微信公众号,订阅我的博客!
坚持原创技术分享,您的支持将鼓励我继续创作!

热评文章