前言
在做项目的过程中,发现经常会使用到对话框这个组件,虽然有方法可以调用,但是在不同的Activity中每次都要重建就显得很麻烦了,为此我封装了一个可供自己使用的对话框组件,一段代码直接调用即可。
介绍
这是一个快速调用对话框的库
平台要求
- Android SDK 15+
基本使用
- 在你的配置文件中添加 JitPack的代码仓库
- Gradle
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
- Maven
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
- 添加依赖
- Gradle
dependencies {
compile 'com.github.YueYongDev:SixSystemDialog:v1.0'
}
- Maven
<dependency>
<groupId>com.github.YueYongDev</groupId>
<artifactId>SixSystemDialog</artifactId>
<version>v1.0</version>
</dependency>
- 调用
- 普通对话框(两个按钮)
SixDialog.showNormalDialog(mContext, title, message, positiveBtnMsg, negativeBtnMsg,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, positiveBtnMsg, Toast.LENGTH_SHORT).show();
}
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, negativeBtnMsg, Toast.LENGTH_SHORT).show();
}
});
- 普通对话框(3个按钮)
SixDialog.showMultiBtnDialog(mContext, title, message, positiveBtnMsg, neutralBtnMsg, negativeBtnMsg, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, positiveBtnMsg, Toast.LENGTH_SHORT).show();
}
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, neutralBtnMsg, Toast.LENGTH_SHORT).show();
}
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, negativeBtnMsg, Toast.LENGTH_SHORT).show();
}
});
- 列表对话框
SixDialog.showListDialog(mContext, title, items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, "你选择了" + items[which], Toast.LENGTH_SHORT).show();
}
});
- 单选对话框
SixDialog.showSingleChoiceDialog(mContext, title, positiveBtnMsg, items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SixDialog.yourChoice = which;
}
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (SixDialog.yourChoice != -1) {
Toast.makeText(mContext, "你选择了" + items[SixDialog.yourChoice], Toast.LENGTH_SHORT).show();
}
}
});
- 多选对话框
SixDialog.showMultiChoiceDialog(mContext, title, positiveBtnMsg, items, initChoiceSets, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
SixDialog.yourChoices.add(which);
} else {
SixDialog.yourChoices.remove(which);
}
}
}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int size = SixDialog.yourChoices.size();
StringBuilder str = new StringBuilder();
for (int i = 0; i < size; i++) {
str.append(items[SixDialog.yourChoices.get(i)]).append(" ");
}
Toast.makeText(MainActivity.this, "你选中了" + str, Toast.LENGTH_SHORT).show();
}
});
- 编辑对话框
SixDialog.showInputDialog(mContext, title, positiveBtnMsg, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mContext, SixDialog.editText.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
- 参数说明
//对话框标题
private String title = "this is title";
//对话框信息
private String message = "this is meaasge";
//对话框确定键的文字
private String positiveBtnMsg = "ok";
//对话框取消键的文字
private String negativeBtnMsg = "cancel";
//对话框中间键的文字
private String neutralBtnMsg = "more";
//对话框列表中的文字
private String[] items = {"item1", "item2", "item3", "item4"};
// initChoiceSets[] 设置默认选中的选项,全为false默认均未选中
private boolean initChoiceSets[] = {false, false, false, false};
用法例子详见Demo
参考
android 8种对话框(Dialog)使用方法汇总
如何将自己在github写的android library开源,让大家依赖使用