在android中,目前有四种方式创建dialog,分别是AlertDialog ProgressDialog DatePickerDialog TimePickerDialog. 其中AlertDialog 是最通用的对话框,ProgressDialog 用于描述进度信息,DatePickerDialog和TimePickerDialog主要用于日期和时间。
AlertDialog 实例:
可以用使用系统自带的方法
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle("请选择");
builder.setMessage("对话框的内容").
builder.setIcon(R.drawable.ic_launcher)
builder.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
})
builder.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
alertDialog = builder.create();
alertDialog.show();
第二自己写个自定义shrew_exit_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:Android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<!-- 退出游戏的背景图 -->
<ImageView android:id="@+id/exitGameBackground"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/bg_exit_game" />
<!-- 确认按钮 -->
<ImageButton android:layout_alignBottom="@+id/exitGameBackground"
android:layout_alignLeft="@+id/exitGameBackground"
android:layout_marginBottom="30dp"
android:layout_marginLeft="35dp"
android:id="@+id/btn_ok"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/btn_ok" />
<!-- 取消按钮 -->
<ImageButton android:layout_alignBottom="@+id/exitGameBackground"
android:layout_alignRight="@+id/exitGameBackground"
android:layout_marginBottom="30dp"
android:layout_marginRight="35dp"
android:id="@+id/btn_cancel"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/btn_cancel" />
</RelativeLayout>
加载方式一:
Window window =alertDialog.getWindow();
// *** 主要就是在这里实现这种效果的.
// 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
window.setContentView(R.layout.shrew_exit_dialog);
// 为确认按钮添加事件,执行退出应用操作
ImageButton ok = (ImageButton) window.findViewById(R.id.btn_ok);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
exitApp(); // 退出应用...
}
加载方式二:
View view = View.inflate(mContext, R.layout.share_alert, null);
builder.setView(view);
隐藏对话框方法: dismiss(),cancle();
ps:如果尚没显示对话框,调用 dismiss()会引发异常。所以调用之前判断是否显示了;
progrseeDialog 运用
protected ProgressDialog mPdialog;
protected void hideDialog(){
if (mPdialog!=null && mPdialog.isShowing()) {
mPdialog.cancel();
mPdialog.dismiss();
}
}
@Override
protected Dialog onCreateDialog(int id) {
if (id == PROGREE_DIALOG) {
if (mPdialog != null) {
return mPdialog;
}
mPdialog = new ProgressDialog(this);
mPdialog.setMessage("Please wait while loading...");
mPdialog.setIndeterminate(true);
mPdialog.setCancelable(false);
return mPdialog;
}
return null;
}
显示:showDialog()
设置风格:环形模式STYLE_SPINNER 进度条:STYLE_HORIZONTAL
dialog = new DatePickerDialog(this,
dateListener,
calendar.get(Calendar.YEAR),
calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
this 表示当前 Acitivity,表示这是当前Acitivity的对话框
TimePickerDialog为时间选择对话框,实现代码如下:
dialog = new TimePickerDialog(this, timeListener,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
false); //是否为二十四制
timeListener 是一个TimePickerDialog.OnTimeSetListener 实例。