android各种提示Dialog 弹出框

举报
再见孙悟空_ 发表于 2022/01/13 23:02:00 2022/01/13
【摘要】 开发过程中 经常需要各种弹出框 用来做提示  或者 展示一些 数据信息。写了一个 DialogUtil , 话不多说 直接上代码 ,希望对大家有帮助。 public static Dialog getProcessDialog(Activity activity, CharSequence message, ...

开发过程中 经常需要各种弹出框 用来做提示  或者 展示一些 数据信息。写了一个 DialogUtil , 话不多说 直接上代码 ,希望对大家有帮助。


   
  1. public static Dialog getProcessDialog(Activity activity, CharSequence message,
  2. boolean dismissTouchOutside, boolean cancelable) {
  3. final LayoutInflater inflater = LayoutInflater.from(activity);
  4. View v = inflater.inflate(R.layout.progress_hud, null);
  5. Dialog dialog = getCustomDialog(activity, v, dismissTouchOutside, cancelable, -1);
  6. if (dismissTouchOutside) {
  7. dialog.setCanceledOnTouchOutside(true);
  8. } else {
  9. dialog.setCanceledOnTouchOutside(false);
  10. }
  11. ImageView spinner = (ImageView) v.findViewById(R.id.spinnerImageView);
  12. ((AnimationDrawable) spinner.getBackground()).start();
  13. TextView messageTv = (TextView) v.findViewById(R.id.message);
  14. if (TextUtils.isEmpty(message)) {
  15. messageTv.setVisibility(View.GONE);
  16. } else {
  17. messageTv.setText(message);
  18. messageTv.setVisibility(View.VISIBLE);
  19. }
  20. return dialog;
  21. }
  22. public static Dialog getCustomDialog(final Activity activity, View view, boolean dismissTouchOutside,
  23. boolean cancelable, int theme) {
  24. //Dialog dialog = theme > 0 ? new FullScreenDialogHUD(activity, theme)
  25. // : new Dialog(activity, R.style.Dialog_FullScreen);
  26. Dialog dialog = new Dialog(activity, R.style.Dialog_FullScreen);
  27. dialog.setContentView(view);
  28. dialog.setCancelable(cancelable);
  29. dialog.setCanceledOnTouchOutside(dismissTouchOutside);
  30. if (!cancelable) {
  31. dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
  32. @Override
  33. public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
  34. if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
  35. dialog.dismiss();
  36. activity.finish();
  37. }
  38. return false;
  39. }
  40. });
  41. }
  42. return dialog;
  43. }
  44. public static Dialog getOneButtonDialog(final Activity activity, String content,
  45. boolean dismissTouchOutside, boolean cancelable,
  46. DialogInterface.OnClickListener confirmOnClickListener) {
  47. Dialog dialog = new AlertDialog.Builder(activity)
  48. .setPositiveButton(R.string.confirm, confirmOnClickListener)
  49. .setCancelable(cancelable)
  50. .setMessage(content)
  51. .create();
  52. dialog.setCanceledOnTouchOutside(dismissTouchOutside);
  53. if (!cancelable) {
  54. dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
  55. @Override
  56. public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
  57. if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
  58. dialog.dismiss();
  59. activity.finish();
  60. }
  61. return false;
  62. }
  63. });
  64. }
  65. return dialog;
  66. }
  67. public static Dialog getButtonsDialog(Activity activity, int resId,
  68. DialogInterface.OnClickListener confirmOnClickListener) {
  69. return getButtonsDialog(activity, activity.getString(resId), true, true, confirmOnClickListener,
  70. null);
  71. }
  72. public static Dialog getButtonsDialog(Activity activity, String content,
  73. DialogInterface.OnClickListener confirmOnClickListener) {
  74. return getButtonsDialog(activity, content, true, true, confirmOnClickListener, null);
  75. }
  76. public static Dialog getButtonsDialog(Activity activity, String content, boolean dismissTouchOutside,
  77. boolean cancelable, DialogInterface.OnClickListener confirmOnClickListener,
  78. DialogInterface.OnClickListener cancelOnClickListener) {
  79. Dialog dialog = new AlertDialog.Builder(activity)
  80. .setNegativeButton(R.string.cancel, cancelOnClickListener)
  81. .setPositiveButton(R.string.confirm, confirmOnClickListener)
  82. .setCancelable(cancelable)
  83. .setMessage(content)
  84. .create();
  85. dialog.setCanceledOnTouchOutside(dismissTouchOutside);
  86. return dialog;
  87. }
  88. progress_hud.xml
  89. <?xml version="1.0" encoding="utf-8"?>
  90. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  91. android:layout_width="wrap_content"
  92. android:layout_height="wrap_content"
  93. android:orientation="vertical"
  94. android:gravity="center_horizontal"
  95. android:background="@android:color/transparent">
  96. <ImageView
  97. android:id="@+id/spinnerImageView"
  98. android:layout_width="wrap_content"
  99. android:layout_height="wrap_content"
  100. android:background="@anim/spinner"/>
  101. <TextView
  102. android:id="@+id/message"
  103. android:layout_width="wrap_content"
  104. android:layout_height="wrap_content"
  105. android:layout_marginTop="15dp"
  106. android:textColor="#FFFFFF"/>
  107. </LinearLayout>
  108. style Dialog_FullScreen
  109. <style name="Dialog.FullScreen" parent="Theme.AppCompat.Dialog">
  110. <item name="android:windowFrame">@null</item>
  111. <item name="android:windowIsFloating">true</item>
  112. <item name="android:windowContentOverlay">@android:color/transparent</item>
  113. <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
  114. <item name="android:windowIsTranslucent">true</item>
  115. <item name="android:windowNoTitle">true</item>
  116. <item name="android:windowFullscreen">true</item>
  117. <item name="android:windowBackground">@android:color/transparent</item>
  118. <item name="android:backgroundDimEnabled">true</item>
  119. </style>




有这些  其他弹出个人信息框 都可以自定义用getCustomDialog这个方法 传 对应的 view 实现。很方便。
希望对大家有帮助。

文章来源: wukong.blog.csdn.net,作者:再见孙悟空_,版权归原作者所有,如需转载,请联系作者。

原文链接:wukong.blog.csdn.net/article/details/50536915

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。