欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Dialog对话框详解

程序员文章站 2024-02-26 12:14:28
废话不多说了,直接给大家贴代码了。 布局文件xml:

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

布局文件xml:

<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:

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:

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:

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:

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:

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:

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();
}
});
}

自定义布局:

<?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对话框详解小编就给大家介绍这么多,希望对大家有所帮助!