refer:http://www.2cto.com/kf/201205/131876.html

简介

AlertDialog的构造方法全部是Protected的,所以不能直接通过new一个AlertDialog来创建出一个AlertDialog。

要创建一个AlertDialog,就要用到AlertDialog.Builder中的create()方法。

使用AlertDialog.Builder创建对话框需要了解以下几个方法:

setTitle :为对话框设置标题 
setIcon :为对话框设置图标 
setMessage:为对话框设置内容 
setView :给对话框设置自定义样式 
setItems :设置对话框要显示的一个list,一般用于显示几个命令时 
setMultiChoiceItems :用来设置对话框显示一系列的复选框 
setNeutralButton    :普通按钮

setPositiveButton   :给对话框添加"Yes"按钮 
setNegativeButton :对话框添加"No"按钮

setCancelable();// 设置取消功能:设置为true表示取消功能可以使用,设置为false表示取消功能不可用(强制做某事)
//设置为true时可以 给对话框添加取消监听器,用户取消对话框时调用此函数
builder.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface dialog) {

}
});

dismiss:关闭对话框(Dialog类中的)
create :创建对话框 
show :显示对话框

一、简单的AlertDialog

下面,创建一个简单的ALertDialog并显示它:

package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  Dialog alertDialog = newAlertDialog.Builder(this).  setTitle("对话框的标题").  setMessage("对话框的内容").  setIcon(R.drawable.ic_launcher).  create();  alertDialog.show();  }
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);Dialog alertDialog = new AlertDialog.Builder(this). setTitle("对话框的标题"). setMessage("对话框的内容"). setIcon(R.drawable.ic_launcher). create(); alertDialog.show();
} 

}运行结果如下:

二、带按钮的AlertDialog

上面的例子很简单,下面我们在这个AlertDialog上面加几个Button,实现删除操作的提示对话框

package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  Dialog alertDialog = newAlertDialog.Builder(this).  setTitle("确定删除?").  setMessage("您确定删除该条信息吗?").  setIcon(R.drawable.ic_launcher).  setPositiveButton("确定",new DialogInterface.OnClickListener() {  @Override  public void onClick(DialogInterface dialog, int which) {  // TODO Auto-generated method stub   }  }).  setNegativeButton("取消",new DialogInterface.OnClickListener() {  @Override  public void onClick(DialogInterface dialog, int which) {  // TODO Auto-generated method stub   }  }).  setNeutralButton("查看详情", new DialogInterface.OnClickListener() {  @Override  public void onClick(DialogInterface dialog, int which) {  // TODO Auto-generated method stub   }  }).  create();  alertDialog.show();  }
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);Dialog alertDialog = new AlertDialog.Builder(this). setTitle("确定删除?"). setMessage("您确定删除该条信息吗?"). setIcon(R.drawable.ic_launcher). setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which){ // TODO Auto-generated method stub } }). setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which){ // TODO Auto-generated method stub } }). setNeutralButton("查看详情", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which){ // TODO Auto-generated method stub } }). create(); alertDialog.show();
} 

在这个例子中,我们定义了三个按钮,分别是"Yes"按钮,"No"按钮以及一个普通按钮,每个按钮都有onClick事件,TODO的地方可以放点了按钮之后想要做的一些处理

看一下运行结果:

可以看到三个按钮添加到了AlertDialog上,三个没有添加事件处理的按钮,点了只是关闭对话框,没有任何其他操作。

三、类似ListView的AlertDialog

用 setItems(CharSequence[]items, final OnClickListener listener) 方法来实现类似 ListView 的 AlertDialog

第一个参数是要显示的数据的数组,第二个参数是点击某个item的触发事件

package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  final String[] arrayFruit = newString[] { "苹果","橘子","草莓","香蕉"};  Dialog alertDialog = newAlertDialog.Builder(this).  setTitle("你喜欢吃哪种水果?").  setIcon(R.drawable.ic_launcher)  .setItems(arrayFruit, new DialogInterface.OnClickListener() {  @Override  public void onClick(DialogInterface dialog, int which) {  Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[which],Toast.LENGTH_SHORT).show();  }  }).  setNegativeButton("取消",new DialogInterface.OnClickListener() {  @Override  public void onClick(DialogInterface dialog, int which) {  // TODO Auto-generated method stub   }  }).  create();  alertDialog.show();  }
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);final String[] arrayFruit = new String[] { "苹果", "橘子","草莓","香蕉"};Dialog alertDialog = new AlertDialog.Builder(this). setTitle("你喜欢吃哪种水果?"). setIcon(R.drawable.ic_launcher) .setItems(arrayFruit, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which){ Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[which],Toast.LENGTH_SHORT).show(); } }). setNegativeButton("取消", new DialogInterface.OnClickListener() {@Override public void onClick(DialogInterface dialog, int which){ // TODO Auto-generated method stub } }). create(); alertDialog.show();
}
}

运行结果如下:

四、类似RadioButton的AlertDialog

用 setSingleChoiceItems(CharSequence[]items, int checkedItem, final OnClickListener listener) 方法来实现类似 RadioButton 的 AlertDialog

第一个参数是要显示的数据的数组,第二个参数是初始值(初始被选中的item),第三个参数是点击某个item的触发事件

在这个例子里面我们设了一个selectedFruitIndex用来记住选中的item的index

package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {  private int selectedFruitIndex = 0;  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  final String[] arrayFruit = newString[] { "苹果","橘子","草莓","香蕉"};  Dialog alertDialog = newAlertDialog.Builder(this).  setTitle("你喜欢吃哪种水果?").  setIcon(R.drawable.ic_launcher)  .setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener(){  @Override  public void onClick(DialogInterface dialog, int which) {  selectedFruitIndex = which;  }  }).  setPositiveButton("确认",new DialogInterface.OnClickListener() {  @Override  public void onClick(DialogInterface dialog, int which) {  Toast.makeText(Dialog_AlertDialogDemoActivity.this,arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show();  }  }).  setNegativeButton("取消",new DialogInterface.OnClickListener() {  @Override  public void onClick(DialogInterface dialog, int which) {  // TODO Auto-generated method stub   }  }).  create();  alertDialog.show();  }
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
private int selectedFruitIndex = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);final String[] arrayFruit = new String[] { "苹果", "橘子","草莓","香蕉"};Dialog alertDialog = new AlertDialog.Builder(this). setTitle("你喜欢吃哪种水果?"). setIcon(R.drawable.ic_launcher) .setSingleChoiceItems(arrayFruit, 0, newDialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which){ selectedFruitIndex = which; } }). setPositiveButton("确认", new DialogInterface.OnClickListener() {@Override public void onClick(DialogInterface dialog, int which){ Toast.makeText(Dialog_AlertDialogDemoActivity.this,arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show(); } }). setNegativeButton("取消", new DialogInterface.OnClickListener() {@Override public void onClick(DialogInterface dialog, int which){ // TODO Auto-generated method stub } }). create(); alertDialog.show();
}
}

运行结果如下:

五、类似CheckBox的AlertDialog

用 setMultiChoiceItems(CharSequence[]items, boolean[] checkedItems, final OnMultiChoiceClickListener listener) 方法来实现类似 CheckBox 的 AlertDialog 
第一个参数是要显示的数据的数组,第二个参数是选中状态的数组,第三个参数是点击某个 item 的触发事件

package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  final String[] arrayFruit = newString[] { "苹果","橘子","草莓","香蕉"};  final boolean[] arrayFruitSelected =new boolean[] {true, true, false, false};  Dialog alertDialog = newAlertDialog.Builder(this).  setTitle("你喜欢吃哪种水果?").  setIcon(R.drawable.ic_launcher)  .setMultiChoiceItems(arrayFruit, arrayFruitSelected, newDialogInterface.OnMultiChoiceClickListener() {  @Override  public void onClick(DialogInterface dialog, int which, boolean isChecked){  arrayFruitSelected[which] = isChecked;  }  }).  setPositiveButton("确认",new DialogInterface.OnClickListener() {  @Override  public void onClick(DialogInterface dialog, int which) {  StringBuilder stringBuilder = new StringBuilder();  for (int i = 0; i < arrayFruitSelected.length; i++) {  if (arrayFruitSelected[i] == true)  {  stringBuilder.append(arrayFruit[i] + "、");  }  }  Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(),Toast.LENGTH_SHORT).show();  }  }).  setNegativeButton("取消",new DialogInterface.OnClickListener() {  @Override  public void onClick(DialogInterface dialog, int which) {  // TODO Auto-generated method stub   }  }).  create();  alertDialog.show();  }
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);final String[] arrayFruit = new String[] { "苹果", "橘子", "草莓", "香蕉" }; final boolean[] arrayFruitSelected = new boolean[] {true, true, false,false};Dialog alertDialog = new AlertDialog.Builder(this). setTitle("你喜欢吃哪种水果?"). setIcon(R.drawable.ic_launcher) .setMultiChoiceItems(arrayFruit, arrayFruitSelected, newDialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which,boolean isChecked) { arrayFruitSelected[which] = isChecked; } }). setPositiveButton("确认", new DialogInterface.OnClickListener() {@Override public void onClick(DialogInterface dialog, int which){ StringBuilder stringBuilder = newStringBuilder(); for (int i = 0; i < arrayFruitSelected.length;i++) { if (arrayFruitSelected[i] == true) { stringBuilder.append(arrayFruit[i] +"、"); } } Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(),Toast.LENGTH_SHORT).show(); } }). setNegativeButton("取消", new DialogInterface.OnClickListener() {@Override public void onClick(DialogInterface dialog, int which){ // TODO Auto-generated method stub } }). create(); alertDialog.show();
} 

}运行结果如下:

六、自定义View的AlertDialog

有时候我们不能满足系统自带的 AlertDialog 风格,就比如说我们要实现一个 Login 画面,有用户名和密码,这时我们就要用到自定义 View 的 AlertDialog

先创建Login画面的布局文件

 <?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  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:gravity="center">  <TextView  android:layout_width="0dip"  android:layout_height="wrap_content"  android:layout_weight="1"  android:text="@string/user"/>  <EditText  android:layout_width="0dip"  android:layout_height="wrap_content"  android:layout_weight="1" />  </LinearLayout>  <LinearLayout  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:gravity="center">  <TextView  android:layout_width="0dip"  android:layout_height="wrap_content"  android:layout_weight="1"  android:text="@string/passward" />  <EditText  android:layout_width="0dip"  android:layout_height="wrap_content"  android:layout_weight="1" />  </LinearLayout>
</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 android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center"><TextView android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/user" /><EditText android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout><LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center"><TextView android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/passward" /><EditText android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout>
</LinearLayout> 

然后在Activity里面把Login画面的布局文件添加到AlertDialog上

package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
public class Dialog_AlertDialogDemoActivity extends Activity {  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.main);  // 取得自定义View   LayoutInflater layoutInflater =LayoutInflater.from(this);  View myLoginView = layoutInflater.inflate(R.layout.login,null);  Dialog alertDialog = newAlertDialog.Builder(this).  setTitle("用户登录").  setIcon(R.drawable.ic_launcher).  setView(myLoginView).  setPositiveButton("登录",new DialogInterface.OnClickListener() {  @Override  public void onClick(DialogInterface dialog, int which) {  // TODO Auto-generated method stub   }  }).  setNegativeButton("取消",new DialogInterface.OnClickListener() {  @Override  public void onClick(DialogInterface dialog, int which) {  // TODO Auto-generated method stub   }  }).  create();  alertDialog.show();  }
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);// 取得自定义View LayoutInflater layoutInflater = LayoutInflater.from(this); View myLoginView = layoutInflater.inflate(R.layout.login, null); Dialog alertDialog = new AlertDialog.Builder(this). setTitle("用户登录"). setIcon(R.drawable.ic_launcher). setView(myLoginView). setPositiveButton("登录", new DialogInterface.OnClickListener() {@Override public void onClick(DialogInterface dialog, int which){ // TODO Auto-generated method stub } }). setNegativeButton("取消", new DialogInterface.OnClickListener() {@Override public void onClick(DialogInterface dialog, int which){ // TODO Auto-generated method stub } }). create(); alertDialog.show();
} 

}运行结果如下:

AlertDialog详解相关推荐

  1. Android的AlertDialog详解

    AlertDialog的构造方法全部是Protected的,所以不能直接通过new一个AlertDialog来创建出一个AlertDialog. 要创建一个AlertDialog,就要用到AlertD ...

  2. Android的AlertDialog详解(7种方式)

    需要注意的两点: 1. 在setIcon时,需要使用setTitle方法,否则icon不会显示 2.如果同时调用setMessage 和 setItems(或者setSingleChoiceItems ...

  3. xamarin android alertdialog详解

    说明一下:学习xamarin android一段时间,准备写一些xamarin android相关的例子,alertdialog也是使用的非常多得空间之一,非常感谢鸟巢上的小猪,我也是看着他写的教程学 ...

  4. AlertDialog使用详解

    AlertDialog使用详解 普通的对话框 final Builder builder = new AlertDialog.Builder(this); btn.setOnClickListener ...

  5. Android基础入门教程——2.5.3 AlertDialog(对话框)详解

    Android基础入门教程--2.5.3 AlertDialog(对话框)详解 标签(空格分隔): Android基础入门教程 本节引言: 本节继续给大家带来是显示提示信息的第三个控件AlertDia ...

  6. 【Android 常见控件使用】AlertDialog(对话框)详解

    文章目录 AlertDialog(对话框)详解 本节引言 1.基本使用流程 2.几种常用的对话框使用示例 3.通过Builder的setView()定制显示的AlertDialog AlertDial ...

  7. python编程入门与案例详解pdf-Flutter技术入门与实战 PDF 清晰版

    给大家带来的一篇关于Flutter相关的电子书资源,介绍了关于Flutter入门.Flutter实战.Flutter技术方面的内容,本书是由机械工业出版社出版,格式为PDF,资源大小162.5 MB, ...

  8. android控件使用大全,Android常见控件使用详解

    本文实例为大家分享了六种Android常见控件的使用方法,供大家参考,具体内容如下 1.TextView 主要用于界面上显示一段文本信息 2.Button 用于和用户交互的一个按钮控件 //为Butt ...

  9. 宏锦软件 Android 的 ListView 使用详解

     宏锦软件爱好者在开发Android软件时,对ListView的使用有点陌生,于是翻了许多资料,这里给大家一份比较好的教程,希望有用. 在android开发中ListView是比较常用的组件,它以 ...

最新文章

  1. 排序算法---冒泡排序(java版)
  2. CoordinatorLayout使用全解析
  3. dp问题 -挑战例题 2017-7-24
  4. 机器找不到 libcudnn.so.6
  5. sublime text插件emmet自定义模板
  6. linux chromium安装falsh插件
  7. [★]基于.NET 的 加密 解密 算法总结[二]
  8. 10分钟搭建一套代码质量监控平台,开发从此不敢摸鱼
  9. 如何解决移动硬盘找不到的问题
  10. no cortex-m sw device found 问题解决及JLINK下载Hex程序
  11. arm rtx教程_【RTX操作系统教程】第5章 RTX操作系统库方式移植(超级...
  12. 前端判断文件后缀名_JS - 获取文件后缀,判断文件类型(比如是否为图片格式)...
  13. [电影]《指环王》新老三部曲完全赏析(五军之战)
  14. SSA优化章:SSA优化PID
  15. Radiology:磁共振血管造影(MRA)在脑转移瘤治疗中对血管形态改变的测量
  16. 1080p和1080i有什么区别?
  17. 如何用wps设计统一的图片背景
  18. 2022美国大学生数学建模竞赛报名通知
  19. 关于IDEL中的全局搜索不显示该有的类的解决办法之一
  20. S2B2C模式学习总结

热门文章

  1. 相亲交友App开发解决方案及功能框架
  2. linux之前关闭信号,Linux 两组信号对比(关闭和停止进程信号)
  3. Android手机开发者模式设置
  4. MySQL binlog详解
  5. 当下适合男人用的智能手机集合-转
  6. GIWIFI自动登录脚本
  7. 苹果投屏,IOS投屏
  8. 服务器主板内存插满只显示8g,4条内存槽插满结果电脑狂蓝屏:原因吐血
  9. matlab interface,FREE金融数据Matlab接口(Finance Data Matlab Interface)
  10. NEO-Python