Dialog

几种常见的对话框使用。

确认取消对话框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("约会把...");
builder.setMessage("告别单身, 你愿意吗 ?");
builder.setPositiveButton("愿意,gogogo", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "我也单身, 说不定 可以 来找我...", 0).show();
}
});
builder.setNegativeButton("不愿意", null );
builder.show();

显示效果:

转载请注明出处:http://shenshanlaoyuan.com/2016/05/23/Android/Dialog/

访问原文「Dialog」获取最佳阅读体验并参与讨论

单选对话框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("单选对话框 ");
final String[] items = { "小丽", "小红", "小芳" };
builder.setSingleChoiceItems(items, -1,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,
" 被点击了 : " + items[which] + ",位置: " + which, 0)
.show();
}
});
builder.show();

效果:

多选对话框

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("多选");
final String[] items = { "android", "ios", "javaee", "php", "C++" };
boolean[] checkedItems = { true, true, false, false, false };
builder.setMultiChoiceItems(items, checkedItems,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
Toast.makeText(
MainActivity.this,
" 被点击了 : " + items[which] + ",位置: " + which
+ ", 值是: " + isChecked, 0).show();
}
});
builder.show();

显示效果:

进度对话框

1
2
3
4
5
6
7
8
9
10
11
12
ProgressDialog pd = new ProgressDialog(this);
// 设置对话框的标题
pd.setTitle("任务正在执行中");
// 设置对话框显示的内容
pd.setMessage("任务正在执行中,敬请等待...");
// 设置对话框能用“取消”按钮关闭
pd.setCancelable(true);
// 设置对话框的进度条风格
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// 设置对话框的进度条是否显示进度
pd.setIndeterminate(true);
pd.show();

显示效果:

进度对话框带-进度条的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
final ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("正在下载中。。。");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// progress.setIndeterminate(true);
progress.show();
final int totalProgressTime = 100;
final Thread t = new Thread() {
@Override
public void run() {
int jumpTime = 0;
while (jumpTime < totalProgressTime) {
try {
sleep(200);
jumpTime += 5;
progress.setProgress(jumpTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
t.start();

显示效果:

深山老猿 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
如对你有帮助,请我喝杯咖啡吧!
显示 Gitment 评论