时隔一年,又要准备做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

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

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. 2014.01.21 ——— android 关联android-support源码
  2. 细数Android(安卓)Studio中使用junit4测试框架中的坑
  3. Andorid Dialog 示例【慢慢更新】
  4. Android内容提供者源码
  5. android源码下载方式
  6. 【30篇突击 android】源码统计四
  7. Android(安卓)4.0.1 源码下载,编译和运行
  8. android支持多行的radiogroup
  9. [android源码下载索引贴】微信+二维码那都不是事......

随机推荐

  1. 【转】善用Android预定义样式来为我们的
  2. 那些年收藏的技术文章(一)-CSDN篇
  3. android 中文 api (43) ―― Chronometer
  4. 在android studio 2.1 实现简单的ndk
  5. Android(安卓)liveData 和ViewModel 使用
  6. android万能驱动制作方法
  7. Android从文件目录中写入和读取图片
  8. Android 电话的反射调用机制实现静默接听
  9. android Intent用法归纳
  10. 我使用过的 控件的一些特性(layout_weight