Android中经常会需要在Android界面上弹出一些对话框提示用户,比如App的退出的时候都会有各种框来挽留你的心,支付宝的时候输入密码的密码框,非常常见及其实用的功能,类似于JS中的alter,C#中C/S中常用MessgeBox,总而言之就是一个功能弹,弹,弹,本文就简单的叙述一下Dialog的各种弹框使用及监听,顺便写了一个简单的自定义弹框,接下来请看正文:
一般对话框
先看下整个App页面:
看下一般对话框的结果:
具体代码的实现,其中定义了两个按钮,一个是确定(PositiveButton)按钮,一个是取消(NegativeButton)按钮:
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
|
builder =
new
AlertDialog.Builder(
this
);
builder.setTitle(
"亲,你真的要离开吗?"
);
// 相当于确定
builder.setPositiveButton(
"退出"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.
this
,
"是的,哥要走了"
,
Toast.LENGTH_SHORT).show();
}
});
// 相当于取消
builder.setNegativeButton(
"再看看"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.
this
,
"行,哥再看会"
,
Toast.LENGTH_SHORT).show();
}
});
builder.show();
|
ICON对话框
上面只是用到了两个按钮,其实还有一个是中立按钮,另外发现每次都写一个监听事件感觉太麻烦了,可以单独写一个监听事件去判断哪个事件被触发了:
单独定义个DialogClickListener:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
private
DialogInterface.OnClickListener dialogClickListener =
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
switch
(which) {
case
DialogInterface.BUTTON_POSITIVE:
Toast.makeText(MainActivity.
this
,
"强烈支持"
+ which,
Toast.LENGTH_SHORT).show();
break
;
case
DialogInterface.BUTTON_NEUTRAL:
Toast.makeText(MainActivity.
this
,
"跟我无关,我是中立的"
+ which,
Toast.LENGTH_SHORT).show();
break
;
case
DialogInterface.BUTTON_NEGATIVE:
Toast.makeText(MainActivity.
this
,
"强烈反对"
+ which,
Toast.LENGTH_SHORT).show();
break
;
default
:
break
;
}
}
};
|
按钮事件的调用:
1
2
3
4
5
6
7
8
9
|
builder =
new
AlertDialog.Builder(
this
);
builder.setTitle(
"XX视频"
);
builder.setIcon(android.R.drawable.btn_star);
builder.setMessage(
"如何看待香港占中事件?"
);
builder.setPositiveButton(
"支持"
, dialogClickListener);
builder.setNegativeButton(
"反对"
, dialogClickListener);
// 中立
builder.setNeutralButton(
"中立"
, dialogClickListener);
builder.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
30
31
|
builder =
new
AlertDialog.Builder(
this
);
builder.setTitle(
"NBA幻觉"
);
builder.setIcon(android.R.drawable.btn_star);
final
String[] nameArrStrings =
new
String[] {
"马刺很老"
,
"小牛很弱"
,
"火箭很强"
};
builder.setItems(nameArrStrings,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.
this
, nameArrStrings[which],
Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton(
"确定"
,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
}
});
builder.setNegativeButton(
"取消"
,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
}
});
builder.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
30
|
builder =
new
AlertDialog.Builder(
this
);
builder.setTitle(
"NBA球队"
);
builder.setIcon(android.R.drawable.btn_radio);
final
String[] ballArr =
new
String[] {
"骑士"
,
"热火"
,
"凯尔特人"
};
builder.setSingleChoiceItems(ballArr,
1
,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.
this
, ballArr[which],
Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton(
"确定"
,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
}
});
builder.setNegativeButton(
"取消"
,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
}
});
builder.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
30
31
32
33
34
35
|
builder =
new
AlertDialog.Builder(
this
);
builder.setTitle(
"NBA球星"
);
builder.setIcon(android.R.drawable.btn_radio);
final
String[] ballstartArr =
new
String[] {
"邓肯"
,
"诺维斯基"
,
"莱昂纳德"
,
"科比"
};
final
boolean
[] multiCheckedArrStrings =
new
boolean
[] {
true
,
false
,
true
,
false
};
builder.setMultiChoiceItems(ballstartArr, multiCheckedArrStrings,
new
OnMultiChoiceClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which,
boolean
isChecked) {
// TODO Auto-generated method stub
StringBuffer buffer =
new
StringBuffer();
for
(
int
i =
0
; i < multiCheckedArrStrings.length; i++) {
if
(multiCheckedArrStrings[i]) {
buffer.append(ballstartArr[i] +
","
);
}
}
buffer.deleteCharAt(buffer.length() -
1
);
Toast.makeText(MainActivity.
this
, buffer,
Toast.LENGTH_SHORT).show();
}
});
builder.setPositiveButton(
"确定"
,
new
OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
}
});
builder.show();
|
文本对话框
比如说支付宝,转账的时候你需要输入密码,弹出一个密码输入框,效果如图:
代码实现,,这个时候你需要给builder中的view赋值:
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
|
builder =
new
AlertDialog.Builder(
this
);
builder.setTitle(
"输入密码"
);
builder.setIcon(android.R.drawable.btn_star);
final
EditText editText =
new
EditText(MainActivity.
this
);
builder.setView(editText);
builder.setPositiveButton(
"确定"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.
this
,
"输入的密码是:"
+ editText.getText(),
Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton(
"取消"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.
this
,
"取消"
,
Toast.LENGTH_SHORT).show();
}
});
builder.show();
|
自定义对话框
上面的那个EditText是创建的,其实可以自定义一个布局,然后通过填充进去,效果如下:
自定义一个dialog_text.xml文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:orientation=
"horizontal"
>
<TextView
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"密码:"
/>
<EditText
android:id=
"@+id/edit_password"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
/>
</LinearLayout>
|
代码实现:
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
|
builder =
new
AlertDialog.Builder(
this
);
builder.setTitle(
"输入密码"
);
builder.setIcon(android.R.drawable.btn_star);
final
EditText editText =
new
EditText(MainActivity.
this
);
builder.setView(editText);
builder.setPositiveButton(
"确定"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.
this
,
"输入的密码是:"
+ editText.getText(),
Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton(
"取消"
,
new
DialogInterface.OnClickListener() {
@Override
public
void
onClick(DialogInterface dialog,
int
which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.
this
,
"取消"
,
Toast.LENGTH_SHORT).show();
}
});
builder.show();
|
------------2015.11.15更新---忘记写了,还有一种是进度条对话框,简单的进度对话框和带进度的对话框-----
代码如下:
1
2
3
4
|
ProgressDialog progressDialog=
new
ProgressDialog(
this
);
progressDialog.setTitle(
"XX助手"
);
progressDialog.setMessage(
"XX正在更新。。。"
);
progressDialog.show();
|
显示进度的对话框:
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
final
ProgressDialog precentDialog=
new
ProgressDialog(
this
);
precentDialog.setTitle(
"XX下载"
);
precentDialog.setMessage(
"XX正在更新。。。"
);
precentDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
precentDialog.setMax(
100
);
precentDialog.show();
new
Thread(){
public
void
run() {
for
(
int
i =
0
; i <
100
; i++) {
try
{
Thread.sleep(
50
);
}
catch
(InterruptedException e) {
e.printStackTrace();
}
precentDialog.setProgress(i);
}
precentDialog.dismiss();
};
}.start();
|
本文转自Fly_Elephant博客园博客,原文链接:http://www.cnblogs.com/xiaofeixiang/p/4084056.html,如需转载请自行联系原作者