对话框是人机交互的重要组成部分,android中使用AlertDialog.Builder类来创建对话框,本文详解了各种对话框的创建方法:
ps:本文采用activity托管的方式来创建对话框,即使用onCreateDialog方法来创建。当调用Activity类的showDialog方法时,系统会调用onCreateDialog方法来返回一个dialog,即showDialog将参数传进onCreateDialog方法。如果使用一般的创建方法,则和程序方法里面的做法相同。
main.xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/btnDeleteFile" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="显示确认对话框" />
<Button android:id="@+id/btnSimpleList" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="显示简单列表对话框" />
<Button android:id="@+id/btnSingleChoiceList"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="显示单选列表对话框" />
<Button android:id="@+id/btnMultiChoiceList"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="显示多选列表对话框" />
<Button
android:id="@+id/customerDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示自定义对话框" />
</LinearLayout>
自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="20dp" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:textSize="20dp" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true" />
</LinearLayout>
</LinearLayout>
主程序:
public class Main extends Activity implements OnClickListener {
private final int DIALOG_TWO_BUTTON = 1;
private final int DIALOG_SIMPLE_LIST = 2;
private final int DIALOG_SINGLE_CHOICE_LIST = 3;
private final int DIALOG_MULTI_CHOICE_LIST = 4;
private final int DIALOG_CUSTOMER_DIALOG = 5;
private ListView lv = null;
private String[] provinces = new String[] { "辽宁省", "山东省", "河北省", "福建省",
"广东省", "黑龙江省" };
//setSingleChoiceItems的响应事件
private ButtonOnClick buttonOnClick = new ButtonOnClick(1);
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnDeleteFile:
showDialog(DIALOG_TWO_BUTTON);
break;
case R.id.btnSimpleList:
showDialog(DIALOG_SIMPLE_LIST);
break;
case R.id.btnSingleChoiceList:
showDialog(DIALOG_SINGLE_CHOICE_LIST);
break;
case R.id.btnMultiChoiceList:
showDialog(DIALOG_MULTI_CHOICE_LIST);
break;
case R.id.customerDialog:
showDialog(DIALOG_CUSTOMER_DIALOG);
break;
}
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_TWO_BUTTON:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.question)
.setTitle("是否删除文件")
//三个button setNeutralButton()
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
new AlertDialog.Builder(Main.this)
.setMessage("文件已经被删除.").create()
.show();
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
new AlertDialog.Builder(Main.this)
.setMessage("您已经选择了取消按钮,该文件未被删除.")
.create().show();
}
}).create();
case DIALOG_SIMPLE_LIST:
return new AlertDialog.Builder(this).setTitle("选择省份")
.setItems(provinces, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
final AlertDialog ad = new AlertDialog.Builder(
Main.this)
.setMessage(
"您已经选择了: " + which + ":"
+ provinces[which]).show();
android.os.Handler hander = new android.os.Handler();
hander.postDelayed(new Runnable() {
public void run() {
ad.dismiss();
}
}, 5 * 1000);
}
}).create();
case DIALOG_SINGLE_CHOICE_LIST:
return new AlertDialog.Builder(this).setTitle("选择省份")
.setSingleChoiceItems(provinces, 1, buttonOnClick)
.setPositiveButton("确定", buttonOnClick)
.setNegativeButton("取消", buttonOnClick).create();
case DIALOG_MULTI_CHOICE_LIST:
AlertDialog ad = new AlertDialog.Builder(this)
.setIcon(R.drawable.image)
.setTitle("选择省份")
.setMultiChoiceItems(
provinces,
new boolean[] { false, true, false, true, false,
false },
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog,
int whichButton, boolean isChecked) {
}
})
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
int count = lv.getCount();
String s = "您选择了:";
for (int i = 0; i < provinces.length; i++) {
if (lv.getCheckedItemPositions().get(i))
s += i
+ ":"
+ lv.getAdapter()
.getItem(i) + " ";
}
if (lv.getCheckedItemPositions().size() > 0) {
new AlertDialog.Builder(Main.this)
.setMessage(s).show();
} else {
new AlertDialog.Builder(Main.this)
.setMessage("您未选择任何省份").show();
}
}
}).setNegativeButton("取消", null).create();
lv = ad.getListView();
return ad;
case DIALOG_CUSTOMER_DIALOG:
LinearLayout loginLayout = (LinearLayout) getLayoutInflater()
.inflate(R.layout.login, null);
new AlertDialog.Builder(this)
.setIcon(R.drawable.image)
.setTitle("用户登录")
//通过setView方法设置自定义视图
.setView(loginLayout)
.setPositiveButton("登录",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// 编写处理用户登录的代码
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// 取消用户登录,退出程序
}
}).show();
break;
}
return null;
}
private class ButtonOnClick implements DialogInterface.OnClickListener
{
private int index;
public ButtonOnClick(int index)
{
this.index = index;
}
public void onClick(DialogInterface dialog, int whichButton)
{
if (whichButton >= 0)
{
index = whichButton;
}
else
{
if (whichButton == DialogInterface.BUTTON_POSITIVE)
{
new AlertDialog.Builder(Main.this).setMessage(
"您已经选择了: " + index + ":" + provinces[index]).show();
}
else if (whichButton == DialogInterface.BUTTON_NEGATIVE)
{
new AlertDialog.Builder(Main.this).setMessage("您什么都未选择.")
.show();
}
}
}
}
//调用show方法之前调用
@Override
protected void onPrepareDialog(int id, Dialog dialog)
{
super.onPrepareDialog(id, dialog);
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnDeleteFile = (Button) findViewById(R.id.btnDeleteFile);
Button btnSimpleList = (Button) findViewById(R.id.btnSimpleList);
Button btnSingleChoiceList = (Button) findViewById(R.id.btnSingleChoiceList);
Button btnMultiChoiceList = (Button) findViewById(R.id.btnMultiChoiceList);
Button customerDialog = (Button)findViewById(R.id.customerDialog);
btnDeleteFile.setOnClickListener(this);
btnSimpleList.setOnClickListener(this);
btnSingleChoiceList.setOnClickListener(this);
btnMultiChoiceList.setOnClickListener(this);
customerDialog.setOnClickListener(this);
}
}