自定義樣式 dialog,可設置界面外點擊屏幕外和返回鍵 是否消失 基本用法如下: Utils 工具類封裝: 效果圖: git 源碼下載 To get a Git project into your build: To get a Git project into your build: To ge ...
自定義樣式 dialog,可設置界面外點擊屏幕外和返回鍵 是否消失
基本用法如下:
CustomDialog.Builder customBuilder = new CustomDialog.Builder(context); customBuilder.setTitle(title) .setMessage(content) .setNegativeButton(cancelText, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); if (cancelListener != null) cancelListener.onClick(dialog, which); } }) .setPositiveButton(okText, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); if (okListener != null) okListener.onClick(dialog, which); } }); CustomDialog dialog = customBuilder.create(); dialog.setCancelable(isAutoDissmiss); dialog.show();
Utils 工具類封裝:
CustomDialogUtil.java
/** * Created by dingzuoqiang on 2017/6/20. * Email: [email protected] */ public class CustomDialogUtil { /** * final Context context, * String content, * String okText, * String cancelText, * final DialogInterface.OnClickListener okListener * final DialogInterface.OnClickListener cancelListener * boolean isAutoDissmiss */ public static View normalDialog(final Context context, String title, String content, String okText, String cancelText, final DialogInterface.OnClickListener okListener, final DialogInterface.OnClickListener cancelListener, boolean isAutoDissmiss) { CustomDialog.Builder customBuilder = new CustomDialog.Builder(context); customBuilder.setTitle(title) .setMessage(content) .setNegativeButton(cancelText, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); if (cancelListener != null) cancelListener.onClick(dialog, which); } }) .setPositiveButton(okText, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); if (okListener != null) okListener.onClick(dialog, which); } }); CustomDialog dialog = customBuilder.create(); dialog.setCancelable(isAutoDissmiss); dialog.show(); return dialog.getWindow().getDecorView(); } }
CustomDialogUtil.normalDialog(MainActivity.this, "Title", "啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊", "確定", "取消", onClickListener, null, true);
效果圖:
To get a Git project into your build:
Step 1. Add the JitPack repository to your build file
allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
Step 2. Add the dependency
dependencies { compile 'com.github.dingzuoqiang:CustomDialog:v1.0' }