警告对话框AlertDialog的使用
来源:互联网 发布:淘宝3d试衣技术 编辑:程序博客网 时间:2024/03/28 22:48
AlertDialog跟WIN32开发中的Dialog不一样,AlertDialog是非阻塞的,而阻塞的对话框用的是PopupWindow。
为了创建一个警告对话框,使用AlertDialog.Builder 子类。通过AlertDialog.Builder(Context)获取一个构造器Builder,然后使用这个Builder类的公共方法来定义警告对话框的所有属性。最后通过Builder.Create()来创建AlertDialog对象。
有时可以不调用Builder.Create(),而是在设置好警告对话框的所有属性后直接调用show()显示AlertDialog,如下所示:
if (isExistNotUploadData() && !mApp.getNetWorkManager().isNetWorkCntedOrCnting()){ new AlertDialog.Builder(this).setTitle(R.string.no_network).setMessage(R.string.upload_poweron_prompt_connectnet).setPositiveButton(R.string.pub_setting,//设置“确定”按钮new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int whichButton) {}}).setNegativeButton(android.R.string.cancel,,//设置“取消”按钮new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int whichButton) {dialog.cancel();}}).show();//直接调用Show()显示对话框 } }
一.、 按钮式警告对话框
通过setPositiveButton(...)响应Yes/Ok的点击,setNeutralButton(...)响应中立行为的点击,setNegativeButton(...)响应No/Cancel的点击。注意,只能各自设置一个按钮来响应点击事件。
AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setMessage("Are you sure you want to exit?").setCancelable(false)//设置该对话框为不能使用返回按钮关闭这个对话框.setPositiveButton("Yes",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {}}).setNegativeButton("No", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {dialog.cancel();}});AlertDialog alert = builder.create();//通过Builder.Create()来创建AlertDialog对象alert.show();///获取AlertDialog对象后,调用Show()显示
二、列表式警告对话框
调用setItems()添加一个可选项列表,该列表接受一组显示的items和一个DialogInterface.OnClickListener 来定义用户选中按钮后所采取动作。
final CharSequence[] items = { "Red", "Green", "Blue" };AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("Pick a color");builder.setItems(items, new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int item) {Toast.makeText(getApplicationContext(), items[item],Toast.LENGTH_SHORT).show();}});AlertDialog alert = builder.create();alert.show();();//获取AlertDialog对象后,调用Show()显示
三、复选框或单选框式警告对话框
要在对话框里创建一个多选项列表(checkboxes)或者单选项(radio buttons),可分别调用setMultiChoiceItems() 和setSingleChoiceItems() 方法。
注意: 为了在用户离开或暂停这个活动的时候能够保存选择,你必须通过活动生命期Activity Lifecycle来恰当的保存和恢复设置。为了永久保存选项,即使活动进程被完全终止,你需要使用数据存储Data Storage技术。
final CharSequence[] items = { "Red", "Green", "Blue" };AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("Pick a color");builder.setSingleChoiceItems(items, 2,new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int item) {Toast.makeText(getApplicationContext(), items[item],Toast.LENGTH_SHORT).show();}});AlertDialog alert = builder.create();alert.show();
setSingleChoiceItems() 的第二个参数是一个checkedItem整型数值,指示了缺省选择项的位置,其值定义如下:
-1:表示不选中任何项;
0:表示选中第一项;
1:表示选中第二项;
2:表示选中第三项;
如此类推。
- 警告对话框AlertDialog的使用
- AlertDialog:警告对话框的创建方法
- AlertDialog 警告对话框
- AlertDialog 警告对话框
- AlertDialog对话框的使用
- AlertDialog对话框的使用
- 对话框AlertDialog的使用
- Android中的警告对话框(AlertDialog)使用案例
- Android中的AlertDialog使用示例一(警告对话框)
- Android AlertDialog警告对话框实现
- Android之警告对话框alertdialog
- AlertDialog对话框的简单使用
- 【Android】对话框AlertDialog的使用(一)
- 使用AlertDialog创建对话框的大致步骤
- Android中AlertDialog对话框的使用
- AlertDialog对话框的使用(四种)
- AlertDialog(对话框)的介绍和使用
- Android中AlertDialog(对话框)的使用
- MVC 过滤器Filter
- iPad五大另类功能:解决孩子哭闹等问题
- C语言预处理
- 最终版 Android 3.0 平台和 SDK 工具更新
- Userinit.exe
- 警告对话框AlertDialog的使用
- Windows 7下VS2008破解90天限制的激活升级方法(支持简体中文,英文,繁体中文)
- 格式化I/O函数
- 线程安全的C++的Singleton实现
- 用主体+插件的方式构造软件
- C++/C 出现较多的面试题的答案与评分标准
- Thread.Join()和Thread.Sleep()
- 字符串和内存操作函数
- SQL2005级联删除,层叠,设置空,设置默认值