Android之PopupWindow弹出对话框 Android之PopupWindow弹出对话框

Android之PopupWindow弹出对话框

分类:Android 931人阅读 评论(0) 收藏 举报
Android的对话框常用的有两种:PopupWindow和AlertDialog。PopupWindow顾名思义为弹出菜单,不同于AlertDialog对话框,PopupWindow弹出的位置可以很多变化,按照有无偏移分,可以分为无偏移和偏移两种;按照参照类型不同又可以分为两种:相对某个控件(Anchor锚)的位置和父容器内部的相对位置。具体如下:

函数 简介
showAsDropDown(Viewanchor) 相对某个控件的位置(正左下方),无偏移
showAsDropDown(Viewanchor, int xoff, int yoff) 相对某个控件的位置,有偏移
showAtLocation(Viewparent, int gravity, int x, int y) 父容器容器相对位置,例如正中央Gravity.CENTER,下方Gravity.BOTTOM等

下面是运行程序截图:

Android之PopupWindow弹出对话框 Android之PopupWindow弹出对话框Android之PopupWindow弹出对话框 Android之PopupWindow弹出对话框

Android之PopupWindow弹出对话框 Android之PopupWindow弹出对话框Android之PopupWindow弹出对话框 Android之PopupWindow弹出对话框

程序代码:

布局:main.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/layout"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. >
  8. <TextView
  9. android:id="@+id/tv_showText"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:gravity="center"
  13. android:text="@string/hello"
  14. android:textSize="22px"
  15. />
  16. <Button
  17. android:id="@+id/bt_PopupWindow1"
  18. android:text="以自己为Anchor,不偏移"
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. />
  22. <Button
  23. android:id="@+id/bt_PopupWindow2"
  24. android:text="以自己为Anchor,正下方"
  25. android:layout_width="fill_parent"
  26. android:layout_height="wrap_content"
  27. />
  28. <Button
  29. android:id="@+id/bt_PopupWindow3"
  30. android:text="以屏幕中心为参照,不偏移(正中间)"
  31. android:layout_width="fill_parent"
  32. android:layout_height="wrap_content"
  33. />
  34. <Button
  35. android:id="@+id/bt_PopupWindow4"
  36. android:text="以屏幕下方为参照,下方中间"
  37. android:layout_width="fill_parent"
  38. android:layout_height="wrap_content"
  39. />
  40. </LinearLayout>

自定义对话框dialog.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <TextView
  8. android:id="@+id/tv_tip"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="请输入内容:"
  12. />
  13. <EditText
  14. android:id="@+id/et_text"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. ></EditText>
  18. <LinearLayout
  19. android:gravity="center_horizontal"
  20. android:layout_width="fill_parent"
  21. android:layout_height="fill_parent"
  22. >
  23. <Button
  24. android:id="@+id/bt_ok"
  25. android:text="确定"
  26. android:layout_width="100px"
  27. android:layout_height="50px"
  28. />
  29. <Button
  30. android:id="@+id/bt_cancle"
  31. android:text="取消"
  32. android:layout_width="100px"
  33. android:layout_height="50px"
  34. />
  35. </LinearLayout>
  36. </LinearLayout>

代码:

  1. packagecom.myandroid.test;
  2. importandroid.app.Activity;
  3. importandroid.content.Context;
  4. importandroid.content.SharedPreferences.Editor;
  5. importandroid.os.Bundle;
  6. importandroid.util.Log;
  7. importandroid.view.Gravity;
  8. importandroid.view.LayoutInflater;
  9. importandroid.view.View;
  10. importandroid.view.View.OnClickListener;
  11. importandroid.view.ViewGroup.LayoutParams;
  12. importandroid.widget.Button;
  13. importandroid.widget.EditText;
  14. importandroid.widget.Gallery;
  15. importandroid.widget.PopupWindow;
  16. importandroid.widget.TextView;
  17. publicclassPopupWindowTestextendsActivity{//PopupWindow属于不阻塞的对话框,AlertDialog则是阻塞的。
  18. privateButtonbt_popupWindow1;
  19. privateButtonbt_popupWindow2;
  20. privateButtonbt_popupWindow3;
  21. privateButtonbt_popupWindow4;
  22. privateTextViewtv_showText;
  23. privatePopupWindowpopupWindow;
  24. privateintscreenWidth;
  25. privateintscreenHeight;
  26. privateintdialgoWidth;
  27. privateintdialgoheight;
  28. /**Calledwhentheactivityisfirstcreated.*/
  29. @Override
  30. publicvoidonCreate(BundlesavedInstanceState){
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.main);
  33. initView();
  34. }
  35. /**
  36. *初始化控件和响应事件
  37. */
  38. privatevoidinitView(){
  39. bt_popupWindow1=(Button)findViewById(R.id.bt_PopupWindow1);
  40. bt_popupWindow2=(Button)findViewById(R.id.bt_PopupWindow2);
  41. bt_popupWindow3=(Button)findViewById(R.id.bt_PopupWindow3);
  42. bt_popupWindow4=(Button)findViewById(R.id.bt_PopupWindow4);
  43. tv_showText=(TextView)findViewById(R.id.tv_showText);
  44. bt_popupWindow1.setOnClickListener(newClickEvent());
  45. bt_popupWindow2.setOnClickListener(newClickEvent());
  46. bt_popupWindow3.setOnClickListener(newClickEvent());
  47. bt_popupWindow4.setOnClickListener(newClickEvent());
  48. }
  49. /**
  50. *按钮点击事件处理
  51. *@authorKobi
  52. *
  53. */
  54. privateclassClickEventimplementsOnClickListener{
  55. @Override
  56. publicvoidonClick(Viewv){
  57. //TODOAuto-generatedmethodstub
  58. switch(v.getId()){
  59. caseR.id.bt_PopupWindow1://以自己为Anchor,不偏移
  60. getPopupWindow();
  61. popupWindow.showAsDropDown(v);
  62. break;
  63. caseR.id.bt_PopupWindow2://以自己为Anchor,偏移(screenWidth-dialgoWidth)/2,0)--按钮正下方
  64. getPopupWindow();
  65. popupWindow.showAsDropDown(v,(screenWidth-dialgoWidth)/2,0);
  66. break;
  67. caseR.id.bt_PopupWindow3://以屏幕中心为参照,不偏移
  68. getPopupWindow();
  69. popupWindow.showAtLocation(findViewById(R.id.layout),Gravity.CENTER,0,0);
  70. break;
  71. caseR.id.bt_PopupWindow4://以屏幕左下角为参照,偏移(screenWidth-dialgoWidth)/2,0)--屏幕下方中央
  72. getPopupWindow();
  73. popupWindow.showAtLocation(findViewById(R.id.layout),
  74. Gravity.BOTTOM,0,0);
  75. break;
  76. default:
  77. break;
  78. }
  79. }
  80. }
  81. /**
  82. *创建PopupWindow
  83. */
  84. protectedvoidinitPopuptWindow(){
  85. //TODOAuto-generatedmethodstub
  86. ViewpopupWindow_view=getLayoutInflater().inflate(//获取自定义布局文件dialog.xml的视图
  87. R.layout.dialog,null,false);
  88. popupWindow=newPopupWindow(popupWindow_view,200,150,true);//创建PopupWindow实例
  89. Buttonbt_ok=(Button)popupWindow_view.findViewById(R.id.bt_ok);//dialog.xml视图里面的控件
  90. Buttonbt_cancle=(Button)popupWindow_view.findViewById(R.id.bt_cancle);
  91. finalEditTextet_text=(EditText)popupWindow_view.findViewById(R.id.et_text);
  92. bt_ok.setOnClickListener(newOnClickListener(){
  93. @Override
  94. publicvoidonClick(Viewv){
  95. //TODOAuto-generatedmethodstub
  96. tv_showText.setText(et_text.getText());//在标签里显示内容
  97. popupWindow.dismiss();//对话框消失
  98. }
  99. });
  100. bt_cancle.setOnClickListener(newOnClickListener(){
  101. @Override
  102. publicvoidonClick(Viewv){
  103. //TODOAuto-generatedmethodstub
  104. popupWindow.dismiss();
  105. }
  106. });
  107. //获取屏幕和对话框各自高宽
  108. screenWidth=PopupWindowTest.this.getWindowManager().getDefaultDisplay().getWidth();
  109. screenHeight=PopupWindowTest.this.getWindowManager().getDefaultDisplay().getHeight();
  110. dialgoWidth=popupWindow.getWidth();
  111. dialgoheight=popupWindow.getHeight();
  112. }
  113. /*
  114. *获取PopupWindow实例
  115. */
  116. privatevoidgetPopupWindow(){
  117. if(null!=popupWindow){
  118. popupWindow.dismiss();
  119. return;
  120. }else{
  121. initPopuptWindow();
  122. }
  123. }
  124. @Override
  125. protectedvoidonPause(){
  126. //TODOAuto-generatedmethodstub
  127. super.onPause();
  128. Log.e("ActivityState","onPause");
  129. }
  130. @Override
  131. protectedvoidonResume(){
  132. //TODOAuto-generatedmethodstub
  133. super.onResume();
  134. Log.e("ActivityState","onResume");
  135. }
  136. }
Android的对话框常用的有两种:PopupWindow和AlertDialog。PopupWindow顾名思义为弹出菜单,不同于AlertDialog对话框,PopupWindow弹出的位置可以很多变化,按照有无偏移分,可以分为无偏移和偏移两种;按照参照类型不同又可以分为两种:相对某个控件(Anchor锚)的位置和父容器内部的相对位置。具体如下:

函数 简介
showAsDropDown(Viewanchor) 相对某个控件的位置(正左下方),无偏移
showAsDropDown(Viewanchor, int xoff, int yoff) 相对某个控件的位置,有偏移
showAtLocation(Viewparent, int gravity, int x, int y) 父容器容器相对位置,例如正中央Gravity.CENTER,下方Gravity.BOTTOM等

下面是运行程序截图:

Android之PopupWindow弹出对话框 Android之PopupWindow弹出对话框Android之PopupWindow弹出对话框 Android之PopupWindow弹出对话框

Android之PopupWindow弹出对话框 Android之PopupWindow弹出对话框Android之PopupWindow弹出对话框 Android之PopupWindow弹出对话框

程序代码:

布局:main.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/layout"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. >
  8. <TextView
  9. android:id="@+id/tv_showText"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"
  12. android:gravity="center"
  13. android:text="@string/hello"
  14. android:textSize="22px"
  15. />
  16. <Button
  17. android:id="@+id/bt_PopupWindow1"
  18. android:text="以自己为Anchor,不偏移"
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. />
  22. <Button
  23. android:id="@+id/bt_PopupWindow2"
  24. android:text="以自己为Anchor,正下方"
  25. android:layout_width="fill_parent"
  26. android:layout_height="wrap_content"
  27. />
  28. <Button
  29. android:id="@+id/bt_PopupWindow3"
  30. android:text="以屏幕中心为参照,不偏移(正中间)"
  31. android:layout_width="fill_parent"
  32. android:layout_height="wrap_content"
  33. />
  34. <Button
  35. android:id="@+id/bt_PopupWindow4"
  36. android:text="以屏幕下方为参照,下方中间"
  37. android:layout_width="fill_parent"
  38. android:layout_height="wrap_content"
  39. />
  40. </LinearLayout>

自定义对话框dialog.xml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. <TextView
  8. android:id="@+id/tv_tip"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="请输入内容:"
  12. />
  13. <EditText
  14. android:id="@+id/et_text"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. ></EditText>
  18. <LinearLayout
  19. android:gravity="center_horizontal"
  20. android:layout_width="fill_parent"
  21. android:layout_height="fill_parent"
  22. >
  23. <Button
  24. android:id="@+id/bt_ok"
  25. android:text="确定"
  26. android:layout_width="100px"
  27. android:layout_height="50px"
  28. />
  29. <Button
  30. android:id="@+id/bt_cancle"
  31. android:text="取消"
  32. android:layout_width="100px"
  33. android:layout_height="50px"
  34. />
  35. </LinearLayout>
  36. </LinearLayout>

代码:

  1. packagecom.myandroid.test;
  2. importandroid.app.Activity;
  3. importandroid.content.Context;
  4. importandroid.content.SharedPreferences.Editor;
  5. importandroid.os.Bundle;
  6. importandroid.util.Log;
  7. importandroid.view.Gravity;
  8. importandroid.view.LayoutInflater;
  9. importandroid.view.View;
  10. importandroid.view.View.OnClickListener;
  11. importandroid.view.ViewGroup.LayoutParams;
  12. importandroid.widget.Button;
  13. importandroid.widget.EditText;
  14. importandroid.widget.Gallery;
  15. importandroid.widget.PopupWindow;
  16. importandroid.widget.TextView;
  17. publicclassPopupWindowTestextendsActivity{//PopupWindow属于不阻塞的对话框,AlertDialog则是阻塞的。
  18. privateButtonbt_popupWindow1;
  19. privateButtonbt_popupWindow2;
  20. privateButtonbt_popupWindow3;
  21. privateButtonbt_popupWindow4;
  22. privateTextViewtv_showText;
  23. privatePopupWindowpopupWindow;
  24. privateintscreenWidth;
  25. privateintscreenHeight;
  26. privateintdialgoWidth;
  27. privateintdialgoheight;
  28. /**Calledwhentheactivityisfirstcreated.*/
  29. @Override
  30. publicvoidonCreate(BundlesavedInstanceState){
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.main);
  33. initView();
  34. }
  35. /**
  36. *初始化控件和响应事件
  37. */
  38. privatevoidinitView(){
  39. bt_popupWindow1=(Button)findViewById(R.id.bt_PopupWindow1);
  40. bt_popupWindow2=(Button)findViewById(R.id.bt_PopupWindow2);
  41. bt_popupWindow3=(Button)findViewById(R.id.bt_PopupWindow3);
  42. bt_popupWindow4=(Button)findViewById(R.id.bt_PopupWindow4);
  43. tv_showText=(TextView)findViewById(R.id.tv_showText);
  44. bt_popupWindow1.setOnClickListener(newClickEvent());
  45. bt_popupWindow2.setOnClickListener(newClickEvent());
  46. bt_popupWindow3.setOnClickListener(newClickEvent());
  47. bt_popupWindow4.setOnClickListener(newClickEvent());
  48. }
  49. /**
  50. *按钮点击事件处理
  51. *@authorKobi
  52. *
  53. */
  54. privateclassClickEventimplementsOnClickListener{
  55. @Override
  56. publicvoidonClick(Viewv){
  57. //TODOAuto-generatedmethodstub
  58. switch(v.getId()){
  59. caseR.id.bt_PopupWindow1://以自己为Anchor,不偏移
  60. getPopupWindow();
  61. popupWindow.showAsDropDown(v);
  62. break;
  63. caseR.id.bt_PopupWindow2://以自己为Anchor,偏移(screenWidth-dialgoWidth)/2,0)--按钮正下方
  64. getPopupWindow();
  65. popupWindow.showAsDropDown(v,(screenWidth-dialgoWidth)/2,0);
  66. break;
  67. caseR.id.bt_PopupWindow3://以屏幕中心为参照,不偏移
  68. getPopupWindow();
  69. popupWindow.showAtLocation(findViewById(R.id.layout),Gravity.CENTER,0,0);
  70. break;
  71. caseR.id.bt_PopupWindow4://以屏幕左下角为参照,偏移(screenWidth-dialgoWidth)/2,0)--屏幕下方中央
  72. getPopupWindow();
  73. popupWindow.showAtLocation(findViewById(R.id.layout),
  74. Gravity.BOTTOM,0,0);
  75. break;
  76. default:
  77. break;
  78. }
  79. }
  80. }
  81. /**
  82. *创建PopupWindow
  83. */
  84. protectedvoidinitPopuptWindow(){
  85. //TODOAuto-generatedmethodstub
  86. ViewpopupWindow_view=getLayoutInflater().inflate(//获取自定义布局文件dialog.xml的视图
  87. R.layout.dialog,null,false);
  88. popupWindow=newPopupWindow(popupWindow_view,200,150,true);//创建PopupWindow实例
  89. Buttonbt_ok=(Button)popupWindow_view.findViewById(R.id.bt_ok);//dialog.xml视图里面的控件
  90. Buttonbt_cancle=(Button)popupWindow_view.findViewById(R.id.bt_cancle);
  91. finalEditTextet_text=(EditText)popupWindow_view.findViewById(R.id.et_text);
  92. bt_ok.setOnClickListener(newOnClickListener(){
  93. @Override
  94. publicvoidonClick(Viewv){
  95. //TODOAuto-generatedmethodstub
  96. tv_showText.setText(et_text.getText());//在标签里显示内容
  97. popupWindow.dismiss();//对话框消失
  98. }
  99. });
  100. bt_cancle.setOnClickListener(newOnClickListener(){
  101. @Override
  102. publicvoidonClick(Viewv){
  103. //TODOAuto-generatedmethodstub
  104. popupWindow.dismiss();
  105. }
  106. });
  107. //获取屏幕和对话框各自高宽
  108. screenWidth=PopupWindowTest.this.getWindowManager().getDefaultDisplay().getWidth();
  109. screenHeight=PopupWindowTest.this.getWindowManager().getDefaultDisplay().getHeight();
  110. dialgoWidth=popupWindow.getWidth();
  111. dialgoheight=popupWindow.getHeight();
  112. }
  113. /*
  114. *获取PopupWindow实例
  115. */
  116. privatevoidgetPopupWindow(){
  117. if(null!=popupWindow){
  118. popupWindow.dismiss();
  119. return;
  120. }else{
  121. initPopuptWindow();
  122. }
  123. }
  124. @Override
  125. protectedvoidonPause(){
  126. //TODOAuto-generatedmethodstub
  127. super.onPause();
  128. Log.e("ActivityState","onPause");
  129. }
  130. @Override
  131. protectedvoidonResume(){
  132. //TODOAuto-generatedmethodstub
  133. super.onResume();
  134. Log.e("ActivityState","onResume");
  135. }
  136. }

你可能感兴趣的:(PopupWindow)