对话框是程序运行中的弹出窗口。Android系统提供了四种对话框:警告对话框(AlertDialg)、进度对话框(ProgressDialog)、日期选择对话框(DatePickerDialog)和时间对话框(TimePickerDialog)。在此先讲解AlertDialog,提他对话框在后续章节会进行讲述。
AlertDialog是一个提示窗口,要求用户做出选择。该对话框中一般会有几个选择按钮、标题信息和提示信息。
在程序中创建对话框步骤如下:
1、获得AlertDialog的静态内部类Builder对象,由该类来创建对话框。
2、通过Builder对象设置对话框的标题、按钮以及按钮将要响应的事件。
3、调用Builder的create()方法创建对话框。
4、调用AlertDialog的show()方法显示对话框。
案例一:下面创建了一个具有”是“、”否“按钮和提示信息的对话框,用来确认用户的操作。
package com.xiaomo.ui;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.example.helloworld.R;
public class ALert_Dialog extends Activity {
// 声明TextView对象
private TextView myTextView;
// 声明Button对象
private Button myButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextView = (TextView) findViewById(R.id.main_text);
myButton = (Button) findViewById(R.id.main_button);
// 实例化AlertDailog.Builder对象
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
// 添加按钮的单击事件
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 设置显示信息
builder.setMessage("确定要删除该记录吗?")
.
// 设置确定按钮
setPositiveButton("是",
new DialogInterface.OnClickListener() {
// 单击事件
public void onClick(DialogInterface dialog,
int which) {
// 设置TextView文本
myTextView.setText("删除成功!");
}
}).
// 设置取消按钮
setNegativeButton("否",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
myTextView.setText("取消删除!");
}
});
// 创建对话框
AlertDialog ad = builder.create();
// 显示对话框
ad.show();
}
});
}
}

案例二:下面创建了一个像上下文菜单一样来设置多个条目选项。
package com.xiaomo.ui;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import com.example.helloworld.R;
public class Context_Alert_Dialog extends Activity {
//声明组件
private TextView myTextView;
private Button myButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取组件实例
myTextView = (TextView) findViewById(R.id.main_text);
myButton = (Button) findViewById(R.id.main_button);
//对话框选项数组
final String[] items = { "奶茶", "咖啡", "果汁" };
//实例化AlertDialog.Builder实例
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
//添加按钮单击监听器
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//设置标题和选项
builder.setTitle("请点餐").setItems(items,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
//设置文本内容为选中的选项
myTextView.setText(items[which]);
}
});
//以单项按钮样式显示
// builder.setTitle("请点餐").setSingleChoiceItems(items, -1,
// new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog,
// int which) {
// myTextView.setText(items[which]);
// }
// });
AlertDialog ad = builder.create();
ad.show();
}
});
}
}

出处:
http://blog.csdn.net/cl05300629 作者:伫望碧落
|