Android 对话框(Dialog)大全 建立你自己的对话框(二)

2014-11-24 09:22:27 · 作者: · 浏览: 1
egativeButton("取消", null).show();



图5效果:信息内容是一组多选框


new AlertDialog.Builder(this).setTitle("单选框").setIcon(
android.R.drawable.ic_dialog_info).setSingleChoiceItems(
new String[] { "Item1", "Item2" }, 0,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).setNegativeButton("取消", null).show();



图6效果:信息内容是一组简单列表项


new AlertDialog.Builder(this).setTitle("列表框").setItems(
new String[] { "Item1", "Item2" }, null).setNegativeButton(
"确定", null).show();



图7效果:信息内容是一个自定义的布局


1.布局文件


< xml version="1.0" encoding="utf-8" >
http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="#ffffffff" android:orientation="horizontal"
android:id="@+id/dialog">

android:layout_width="wrap_content"
android:id="@+id/tvname" android:text="姓名:" />

android:layout_width="wrap_content" android:id="@+id/etname" android:minWidth="100dip"/>



2.调用代码


LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.dialog,
(ViewGroup) findViewById(R.id.dialog));


new AlertDialog.Builder(this).setTitle("自定义布局").setView(layout)
.setPositiveButton("确定", null)
.setNegativeButton("取消", null).show();