服务器之家

服务器之家 > 正文

Android Dialog对话框详解

时间:2021-07-02 16:17     来源/作者:jerrylsxu

废话不多说了,直接给大家贴代码了。

布局文件xml:

?
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
36
37
38
39
40
41
42
43
44
45
46
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingbottom="@dimen/activity_vertical_margin"
android:paddingleft="@dimen/activity_horizontal_margin"
android:paddingright="@dimen/activity_horizontal_margin"
android:paddingtop="@dimen/activity_vertical_margin"
tools:context=".dialogactivity" >
<button
android:id="@+id/plaindialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="普通dialog" />
<button
android:id="@+id/plaindialogevent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dialog按钮事件集中处理" />
<button
android:id="@+id/inputdialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入框" />
<button
android:id="@+id/listdialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="列表对话框" />
<button
android:id="@+id/radiodialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="单选对话框" />
<button
android:id="@+id/checkboxdialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多选对话框" />
<button
android:id="@+id/diydialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义布局对话框" />
</linearlayout>

activity文件:

普通的dialog:

?
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
private void plaindialogdemo() {
button plainbtn = (button) findviewbyid(r.id.plaindialog);
plainbtn.setonclicklistener(new onclicklistener() {
public void onclick(view v) {
new alertdialog.builder(dialogactivity.this)
.settitle("删除")
.setmessage("确定删除指定数据")
.setpositivebutton("确定",
new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialog,
int which) {
toast.maketext(getapplicationcontext(),
"确定了", toast.length_short)
.show();
}
})
.setnegativebutton("取消",
new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialog,
int which) {
}
}).setcancelable(false).show();
}
});
}

效果如下:

 Android Dialog对话框详解

输入文本框的dialog:

?
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
private void inputdialog() {
button inputbtn = (button) findviewbyid(r.id.inputdialog);
inputbtn.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
// todo auto-generated method stub
final edittext et = new edittext(dialogactivity.this);
new alertdialog.builder(dialogactivity.this)
.settitle("请输入数字")
.setview(et)
.setpositivebutton("确定",
new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialog,
int which) {
// todo auto-generated method stub
toast.maketext(getapplicationcontext(),
et.gettext(),
toast.length_short).show();
}
}).setnegativebutton("取消", null)
.setcancelable(false).show();
}
});
}

效果如下:

Android Dialog对话框详解

列表dialog:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private void listdialogdemo() {
button listbtn = (button) findviewbyid(r.id.listdialog);
listbtn.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
final string[] names = { "c罗", "j罗", "h罗" };
new alertdialog.builder(dialogactivity.this).settitle("列表对话框")
.setitems(names, new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialog,
int which) {
toast.maketext(dialogactivity.this,
names[which], toast.length_short)
.show();
}
}).setnegativebutton("取消", null).show();
}
});
}

效果如下:

Android Dialog对话框详解

单选dialog:

?
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
private void radiodialogdemo() {
button radiobutton = (button) findviewbyid(r.id.radiodialog);
radiobutton.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
final string[] names = { "c罗", "j罗", "h罗" };
new alertdialog.builder(dialogactivity.this)
.settitle("列表对话框")
.setsinglechoiceitems(names, ,
new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialog,
int which) {
selectename = names[which];
}
})
.setpositivebutton("确定",
new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialog,
int which) {
toast.maketext(dialogactivity.this,
selectename, toast.length_short)
.show();
}
}).setnegativebutton("取消", null).show();
}
});
}

效果如下:

Android Dialog对话框详解

多选dialog:

?
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
36
private void checkdialogdemo() {
button checkbtn = (button) findviewbyid(r.id.checkboxdialog);
checkbtn.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
final string[] names = { "c罗", "j罗", "h罗" };
final boolean[] selected = new boolean[] { true, false, true };
new alertdialog.builder(dialogactivity.this)
.setmultichoiceitems(
names,
selected,
new dialoginterface.onmultichoiceclicklistener() {
@override
public void onclick(dialoginterface dialog,
int which, boolean ischecked) {
}
})
.setpositivebutton("确定",
new dialoginterface.onclicklistener() {
@override
public void onclick(dialoginterface dialog,
int which) {
stringbuilder sb = new stringbuilder(
"你选择了:");
for (int i = ; i < names.length; i++) {
if (selected[i]) {
sb.append(names[i]);
}
}
toast.maketext(dialogactivity.this,
sb.tostring(), ).show();
}
}).setnegativebutton("取消", null).show();
}
});
}

效果如下:

Android Dialog对话框详解

自定义dialog:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private void customdialogdemo() {
final alertdialog dlg = new alertdialog.builder(this).create();
dlg.show();
window window = dlg.getwindow();
window.setcontentview(r.layout.diylayout);
imagebutton ok = (imagebutton) window.findviewbyid(r.id.btnok);
ok.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view v) {
toast.maketext(getapplicationcontext(), "关闭了",
toast.length_short).show();
dlg.dismiss();
}
});
}

自定义布局:

?
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
<?xml version="." encoding="utf-"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<imageview
android:id="@+id/dialogimg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerinparent="true"
android:src="@drawable/dialog_bg" />
<textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignleft="@id/dialogimg"
android:layout_aligntop="@id/dialogimg"
android:layout_marginleft="dp"
android:layout_margintop="dp"
android:text="自定义的dialog" />
<imagebutton
android:id="@+id/btnok"
android:layout_width="dp"
android:layout_height="dp"
android:layout_alignright="@id/dialogimg"
android:layout_aligntop="@id/dialogimg"
android:layout_marginright="dp"
android:layout_margintop="dp"
android:background="@drawable/close_dialog" />
</relativelayout>

效果如:

Android Dialog对话框详解

有关android dialog对话框详解小编就给大家介绍这么多,希望对大家有所帮助!

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部