android的dialog分为AlertDialog、ProgressDialog、自定义Dialog
一、AlertDialog
①普通按钮
/* @setIcon 设置对话框图标
* @setTitle 设置对话框标题
* @setMessage 设置对话框消息提示
* setXXX方法返回Dialog对象,因此可以链式设置属性
*/
AlertDialog.Builder normalDialog =
new
AlertDialog.Builder(MainActivity.
this
);
normalDialog.setIcon(R.drawable.icon_dialog);
normalDialog.setTitle(
"我是一个普通Dialog"
)
normalDialog.setMessage(
"你要点击哪一个按钮呢?"
);
normalDialog.setPositiveButton(
"确定"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
//...To-do
}
});
normalDialog.setNegativeButton(
"关闭"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
//...To-do
}
});
// 显示
normalDialog.show();
②列表:
String[] items = {
"我是1"
,
"我是2"
,
"我是3"
,
"我是4"
};
AlertDialog.Builder listDialog =
new
AlertDialog.Builder(MainActivity.
this
);
listDialog.setTitle(
"我是一个列表Dialog"
);
listDialog.setItems(items,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// which 下标从0开始
// ...To-do
Toast.makeText(MainActivity.
this
,
"你点击了"
+ items[which],
Toast.LENGTH_SHORT).show();
}
});
listDialog.show();
③单选
final
String[] items = {
"我是1"
,
"我是2"
,
"我是3"
,
"我是4"
};
yourChoice = -
1
;
AlertDialog.Builder singleChoiceDialog =
new
AlertDialog.Builder(MainActivity.
this
);
singleChoiceDialog.setTitle(
"我是一个单选Dialog"
);
// 第二个参数是默认选项,此处设置为0
singleChoiceDialog.setSingleChoiceItems(items,
0
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
yourChoice = which;
}
});
singleChoiceDialog.setPositiveButton(
"确定"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
if
(yourChoice != -
1
) {
Toast.makeText(MainActivity.
this
,
"你选择了"
+ items[yourChoice],
Toast.LENGTH_SHORT).show();
}
}
});
singleChoiceDialog.show();
④多选
final
String[] items = {
"我是1"
,
"我是2"
,
"我是3"
,
"我是4"
};
// 设置默认选中的选项,全为false默认均未选中
final
boolean
initChoiceSets[]={
false
,
false
,
false
,
false
};
yourChoices.clear();
AlertDialog.Builder multiChoiceDialog =
new
AlertDialog.Builder(MainActivity.
this
);
multiChoiceDialog.setTitle(
"我是一个多选Dialog"
);
multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
new
DialogInterface.OnMultiChoiceClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which,
boolean
isChecked) {
if
(isChecked) {
yourChoices.add(which);
}
else
{
yourChoices.remove(which);
}
}
});
multiChoiceDialog.setPositiveButton(
"确定"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
int
size = yourChoices.size();
String str =
""
;
for
(
int
i =
0
; i < size; i++) {
str += items[yourChoices.get(i)] +
" "
;
}
Toast.makeText(MainActivity.
this
,
"你选中了"
+ str,
Toast.LENGTH_SHORT).show();
}
});
multiChoiceDialog.show();
⑤编辑
/*@setView 装入一个EditView
*/
final
EditText editText =
new
EditText(MainActivity.
this
);
AlertDialog.Builder inputDialog =
new
AlertDialog.Builder(MainActivity.
this
);
inputDialog.setTitle(
"我是一个输入Dialog"
).setView(editText);
inputDialog.setPositiveButton(
"确定"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
Toast.makeText(MainActivity.
this
,
editText.getText().toString(),
Toast.LENGTH_SHORT).show();
}
}).show();
二、ProgressDialog
①圆圈
ProgressDialog waitingDialog=
new
ProgressDialog(MainActivity.
this
);
waitingDialog.setTitle(
"我是一个等待Dialog"
);
waitingDialog.setMessage(
"等待中..."
);
waitingDialog.setIndeterminate(
true
);
waitingDialog.setCancelable(
false
);
waitingDialog.show();
②横向
/* @setProgress 设置初始进度
* @setProgressStyle 设置样式(水平进度条)
* @setMax 设置进度最大值
*/
final
int
MAX_PROGRESS =
100
;
final
ProgressDialog progressDialog =
new
ProgressDialog(MainActivity.
this
);
progressDialog.setProgress(
0
);
progressDialog.setTitle(
"我是一个进度条Dialog"
);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(MAX_PROGRESS);
progressDialog.show();
/* 模拟进度增加的过程
* 新开一个线程,每个100ms,进度增加1
*/
new
Thread(
new
Runnable() {
@Override
public
void
run() {
int
progress=
0
;
while
(progress < MAX_PROGRESS){
try
{
Thread.sleep(
100
);
progress++;
progressDialog.setProgress(progress);
}
catch
(InterruptedException e){
e.printStackTrace();
}
}
// 进度达到最大值后,窗口消失
progressDialog.cancel();
}
}).start();
三、自定义Dialog
①自定义布局
<!-- res/layout/dialog_customize.xml-->
<!-- 自定义View -->
<
LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<
EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</
LinearLayout
>
private
void
showCustomizeDialog() {
/* @setView 装入自定义View ==> R.layout.dialog_customize
* 由于dialog_customize.xml只放置了一个EditView
* dialog_customize.xml可自定义更复杂的View
*/
AlertDialog.Builder customizeDialog =
new
AlertDialog.Builder(MainActivity.
this
);
final
View dialogView = LayoutInflater.from(MainActivity.
this
)
.inflate(R.layout.dialog_customize,
null
);
customizeDialog.setTitle(
"我是一个自定义Dialog"
);
customizeDialog.setView(dialogView);
customizeDialog.setPositiveButton(
"确定"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// 获取EditView中的输入内容
EditText edit_text =
(EditText) dialogView.findViewById(R.id.edit_text);
Toast.makeText(MainActivity.
this
,
edit_text.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});
customizeDialog.show();
}
/**
* 说明:自定义Dialog基类
*/
public abstract class BaseDialog extends Dialog {
private Context context; //下面三个定义的跟上面讲得就是一样的啦
private String title;
public OnItemClickListener onItemClickListener;
protected View view; //看到这里我们定义的就清楚,我们也是借用view这个父类来引入布局的
public BaseDialog(Context context) {
super(context);
this.context=context;
}
public BaseDialog(Context context, int themeResId) {
super(context, themeResId);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setOnItemCheckListener(OnItemClickListener onItemCheckListener) {
if (onItemCheckListener != null)
this.onItemClickListener = onItemCheckListener;
}
protected void init() {
//以view来引入布局
View view = LayoutInflater.from(context).inflate(getLayoutId(), null);
this.view=view;
setContentView(view);
//设置dialog大小
Window dialogWindow = getWindow();
WindowManager manager = ((Activity) context).getWindowManager();
WindowManager.LayoutParams params = dialogWindow.getAttributes(); // 获取对话框当前的参数值
dialogWindow.setGravity(Gravity.BOTTOM);
Display d = manager.getDefaultDisplay(); // 获取屏幕宽、高度
params.width = (int) (d.getWidth() * 0.8); // 宽度设置为屏幕的0.65,根据实际情况调整
dialogWindow.setAttributes(params);
}
//可以看到这里定义了一个抽象方法,这个将交由子类去实现
public abstract int getLayoutId();
}
参考:http://www.jianshu.com/u/8dad2b82bce7