时隔一年,又要准备做Android的开发了,最近复习和整理一下Android的知识。 这次要说的是AlertDialog,这种对话框会经常遇到。AlertDialog跟WIN32开发中的Dialog不一样,AlertDialog是非阻塞的,而阻塞的对话框用的是PopupWindow。

main.xml的源码:

view plain copy to clipboard print ?
  1. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
  3. android:orientation = "vertical"
  4. android:layout_width = "fill_parent"
  5. android:layout_height = "fill_parent"
  6. >
  7. < Button android:id = "@+id/Button01" android:layout_height = "wrap_content" android:text = "非Layout型对话框" android:layout_width = "fill_parent" > </ Button >
  8. < Button android:id = "@+id/Button02" android:layout_height = "wrap_content" android:text = "Layout型对话框" android:layout_width = "fill_parent" > </ Button > < View android:id = "@+id/View01" android:layout_width = "wrap_content" android:layout_height = "wrap_content" > </ View >
  9. </ LinearLayout >

下图是非Layout型对话框,直接使用AlertDialog

Android入门第九篇之AlertDialog

下图是使用了Layout的对话框,可以自定义控件,实现更复杂的对话框

Android入门第九篇之AlertDialog

dialoglayout.xml的源码:

view plain copy to clipboard print ?
  1. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
  3. android:layout_width = "fill_parent" android:layout_height = "wrap_content"
  4. android:orientation = "vertical" >
  5. < EditText android:layout_height = "wrap_content"
  6. android:layout_width = "fill_parent" android:layout_marginLeft = "20dip"
  7. android:layout_marginRight = "20dip" android:textAppearance = "?android:attr/textAppearanceMedium" android:id = "@+id/edtInput" />
  8. </ LinearLayout >

程序源码:

view plain copy to clipboard print ?
  1. package com.testAlertDialog;
  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.os.Bundle;
  7. import android.view.Gravity;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.PopupWindow;
  14. public class testAlertDialog extends Activity{
  15. ButtonbtnShowDialog;
  16. ButtonbtnShowDialog_Layout;
  17. /**Calledwhentheactivityisfirstcreated.*/
  18. @Override
  19. public void onCreate(BundlesavedInstanceState){
  20. super .onCreate(savedInstanceState);
  21. setContentView(R.layout.main);
  22. //定义按钮
  23. btnShowDialog=(Button)this .findViewById(R.id.Button01);
  24. btnShowDialog.setOnClickListener(new ClickEvent());
  25. btnShowDialog_Layout=(Button)this .findViewById(R.id.Button02);
  26. btnShowDialog_Layout.setOnClickListener(new ClickEvent());
  27. }
  28. //统一处理按键事件
  29. class ClickEvent implements OnClickListener{
  30. @Override
  31. public void onClick(Viewv){
  32. //TODOAuto-generatedmethodstub
  33. if (v==btnShowDialog)
  34. showDialog(testAlertDialog.this );
  35. else if (v==btnShowDialog_Layout)
  36. showDialog_Layout(testAlertDialog.this );
  37. }
  38. }
  39. //显示基本的AlertDialog
  40. private void showDialog(Contextcontext){
  41. AlertDialog.Builderbuilder=new AlertDialog.Builder(context);
  42. builder.setIcon(R.drawable.icon);
  43. builder.setTitle("Title" );
  44. builder.setMessage("Message" );
  45. builder.setPositiveButton("Button1" ,
  46. new DialogInterface.OnClickListener(){
  47. public void onClick(DialogInterfacedialog, int whichButton){
  48. setTitle("点击了对话框上的Button1" );
  49. }
  50. });
  51. builder.setNeutralButton("Button2" ,
  52. new DialogInterface.OnClickListener(){
  53. public void onClick(DialogInterfacedialog, int whichButton){
  54. setTitle("点击了对话框上的Button2" );
  55. }
  56. });
  57. builder.setNegativeButton("Button3" ,
  58. new DialogInterface.OnClickListener(){
  59. public void onClick(DialogInterfacedialog, int whichButton){
  60. setTitle("点击了对话框上的Button3" );
  61. }
  62. });
  63. builder.show();
  64. }
  65. //显示基于Layout的AlertDialog
  66. private void showDialog_Layout(Contextcontext){
  67. LayoutInflaterinflater=LayoutInflater.from(this );
  68. final ViewtextEntryView=inflater.inflate(
  69. R.layout.dialoglayout,null );
  70. final EditTextedtInput=(EditText)textEntryView.findViewById(R.id.edtInput);
  71. final AlertDialog.Builderbuilder= new AlertDialog.Builder(context);
  72. builder.setCancelable(false );
  73. builder.setIcon(R.drawable.icon);
  74. builder.setTitle("Title" );
  75. builder.setView(textEntryView);
  76. builder.setPositiveButton("确认" ,
  77. new DialogInterface.OnClickListener(){
  78. public void onClick(DialogInterfacedialog, int whichButton){
  79. setTitle(edtInput.getText());
  80. }
  81. });
  82. builder.setNegativeButton("取消" ,
  83. new DialogInterface.OnClickListener(){
  84. public void onClick(DialogInterfacedialog, int whichButton){
  85. setTitle("" );
  86. }
  87. });
  88. builder.show();
  89. }
  90. }

更多相关文章

  1. 基于源码分析 Android View 绘制机制
  2. Camera源码分析(android2.2)
  3. Android Contacts 联系人源码分析
  4. android应用市场、社区客户端、漫画App、TensorFlow Demo、歌词
  5. android流式布局、待办事项应用、贝塞尔曲线、MVP+Rxjava+Retrof
  6. 如何导入android sdk 的 sample中的源码
  7. Volley 源码解析
  8. 利用半透明对话框实现android运行时的提示界面
  9. Retrofit源码全方面解析

随机推荐

  1. Android(安卓)process与Thread 的问题
  2. android的文件、目录操作
  3. android ListView中Checkbox实现单选,全选
  4. 解决android sdk 无法更新
  5. Android(安卓)8.1 系统锁屏显示流程整理
  6. Android(安卓)SDK下载和更新失败的解决方
  7. [Innost]Android深入浅出之Binder机制
  8. Android(安卓)打开关闭闪光灯工具类
  9. android 调用call
  10. Android系统开机启动流程