经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android 自定义AlertDialog的实现
来源:cnblogs  作者:浮云Cloud  时间:2018/10/19 9:10:34  对本文有异议

 

Android默认的AlertDialog太单调,我们可以通过继承原生的Dialog来实现自定义的Dialog。

 

本文的自定义Dialog和原生的AlertDialog的创建方式类似,通过一个静态Builder类来设置Dialog的图标、标题、内容和按钮。

 

如果想要在Dialog中使用输入框或者其他控件,方法也是类似的,只要写好布局再加载就可以了。

 

效果:

布局文件代码:

(注意这里的根布局的宽高如果用match_parent或者设置为具体的数值都和wrap_conten效果一样,可以通过设置子控件的大小来撑开)

  1. 1 <?xml version="1.0" encoding="utf-8"?>
  2. 2 <android.support.constraint.ConstraintLayout
  3. 3 xmlns:android="http://schemas.android.com/apk/res/android"
  4. 4 xmlns:app="http://schemas.android.com/apk/res-auto"
  5. 5 xmlns:tools="http://schemas.android.com/tools"
  6. 6 android:layout_width="wrap_content"
  7. 7 android:layout_height="wrap_content"
  8. 8 android:background="#ffffff">
  9. 9
  10. 10 <LinearLayout
  11. 11 android:id="@+id/dialog_header"
  12. 12 android:orientation="vertical"
  13. 13 android:layout_width="220dp"
  14. 14 android:layout_height="wrap_content"
  15. 15 android:padding="16dp"
  16. 16 android:gravity="center"
  17. 17 android:background="@color/colorGreen"
  18. 18 app:layout_constraintTop_toTopOf="parent"
  19. 19 app:layout_constraintStart_toStartOf="parent"
  20. 20 app:layout_constraintEnd_toEndOf="parent">
  21. 21
  22. 22 <!-- Icon -->
  23. 23 <ImageView
  24. 24 android:contentDescription="@id/dialog_title"
  25. 25 android:id="@+id/dialog_icon"
  26. 26 android:layout_width="100dp"
  27. 27 android:layout_height="100dp"
  28. 28 android:src="@drawable/ic_check_circle" />
  29. 29
  30. 30 <!-- Titledefault is gone) -->
  31. 31 <TextView
  32. 32 android:id="@+id/dialog_title"
  33. 33 android:layout_width="wrap_content"
  34. 34 android:layout_height="wrap_content"
  35. 35 android:padding="8dp"
  36. 36 android:textSize="18sp"
  37. 37 android:textStyle="bold"
  38. 38 android:textColor="#ffffff"
  39. 39 android:visibility="gone" />
  40. 40
  41. 41 </LinearLayout>
  42. 42
  43. 43 <LinearLayout
  44. 44 android:orientation="vertical"
  45. 45 android:layout_width="wrap_content"
  46. 46 android:layout_height="wrap_content"
  47. 47 android:padding="16dp"
  48. 48 android:gravity="center"
  49. 49 app:layout_constraintTop_toBottomOf="@+id/dialog_header"
  50. 50 app:layout_constraintStart_toStartOf="parent"
  51. 51 app:layout_constraintEnd_toEndOf="parent"
  52. 52 app:layout_constraintBottom_toBottomOf="parent">
  53. 53
  54. 54 <!-- Dialog Message -->
  55. 55 <TextView
  56. 56 android:id="@+id/dialog_message"
  57. 57 android:layout_width="wrap_content"
  58. 58 android:layout_height="wrap_content"
  59. 59 android:padding="8dp"
  60. 60 tools:text="Dialog Message" />
  61. 61
  62. 62 <Button
  63. 63 android:id="@+id/dialog_button"
  64. 64 android:layout_width="100dp"
  65. 65 android:layout_height="42dp"
  66. 66 android:layout_marginTop="16dp"
  67. 67 android:layout_marginBottom="8dp"
  68. 68 android:background="@drawable/bg_dialog_button"
  69. 69 android:textColor="#ffffff"
  70. 70 android:text="@string/dialog_button">
  71. 71
  72. 72 </Button>
  73. 73
  74. 74 </LinearLayout>
  75. 75
  76. 76 </android.support.constraint.ConstraintLayout>

InfoDialog类:

  1. 1 package com.cloud.design.dialog;
  2. 2
  3. 3 import android.app.Dialog;
  4. 4 import android.content.Context;
  5. 5 import android.graphics.Bitmap;
  6. 6 import android.support.annotation.NonNull;
  7. 7 import android.view.LayoutInflater;
  8. 8 import android.view.View;
  9. 9 import android.view.ViewGroup;
  10. 10 import android.widget.Button;
  11. 11 import android.widget.ImageView;
  12. 12 import android.widget.TextView;
  13. 13
  14. 14 import com.cloud.design.R;
  15. 15
  16. 16 public class InfoDialog extends Dialog {
  17. 17
  18. 18 private InfoDialog(Context context, int themeResId) {
  19. 19 super(context, themeResId);
  20. 20 }
  21. 21
  22. 22 public static class Builder {
  23. 23
  24. 24 private View mLayout;
  25. 25
  26. 26 private ImageView mIcon;
  27. 27 private TextView mTitle;
  28. 28 private TextView mMessage;
  29. 29 private Button mButton;
  30. 30
  31. 31 private View.OnClickListener mButtonClickListener;
  32. 32
  33. 33 private InfoDialog mDialog;
  34. 34
  35. 35 public Builder(Context context) {
  36. 36 mDialog = new InfoDialog(context, R.style.Theme_AppCompat_Dialog);
  37. 37 LayoutInflater inflater =
  38. 38 (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  39. 39 //加载布局文件
  40. 40 mLayout = inflater.inflate(R.layout.dialog, null, false);
  41. 41 //添加布局文件到 Dialog
  42. 42 mDialog.addContentView(mLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
  43. 43 ViewGroup.LayoutParams.WRAP_CONTENT));
  44. 44
  45. 45 mIcon = mLayout.findViewById(R.id.dialog_icon);
  46. 46 mTitle = mLayout.findViewById(R.id.dialog_title);
  47. 47 mMessage = mLayout.findViewById(R.id.dialog_message);
  48. 48 mButton = mLayout.findViewById(R.id.dialog_button);
  49. 49 }
  50. 50
  51. 51 /**
  52. 52 * 通过 ID 设置 Dialog 图标
  53. 53 */
  54. 54 public Builder setIcon(int resId) {
  55. 55 mIcon.setImageResource(resId);
  56. 56 return this;
  57. 57 }
  58. 58
  59. 59 /**
  60. 60 * 用 Bitmap 作为 Dialog 图标
  61. 61 */
  62. 62 public Builder setIcon(Bitmap bitmap) {
  63. 63 mIcon.setImageBitmap(bitmap);
  64. 64 return this;
  65. 65 }
  66. 66
  67. 67 /**
  68. 68 * 设置 Dialog 标题
  69. 69 */
  70. 70 public Builder setTitle(@NonNull String title) {
  71. 71 mTitle.setText(title);
  72. 72 mTitle.setVisibility(View.VISIBLE);
  73. 73 return this;
  74. 74 }
  75. 75
  76. 76 /**
  77. 77 * 设置 Message
  78. 78 */
  79. 79 public Builder setMessage(@NonNull String message) {
  80. 80 mMessage.setText(message);
  81. 81 return this;
  82. 82 }
  83. 83
  84. 84 /**
  85. 85 * 设置按钮文字和监听
  86. 86 */
  87. 87 public Builder setButton(@NonNull String text, View.OnClickListener listener) {
  88. 88 mButton.setText(text);
  89. 89 mButtonClickListener = listener;
  90. 90 return this;
  91. 91 }
  92. 92
  93. 93 public InfoDialog create() {
  94. 94 mButton.setOnClickListener(view -> {
  95. 95 mDialog.dismiss();
  96. 96 mButtonClickListener.onClick(view);
  97. 97 });
  98. 98 mDialog.setContentView(mLayout);
  99. 99 mDialog.setCancelable(true); //用户可以点击后退键关闭 Dialog
  100. 100 mDialog.setCanceledOnTouchOutside(false); //用户不可以点击外部来关闭 Dialog
  101. 101 return mDialog;
  102. 102 }
  103. 103 }
  104. 104 }

弹出:

 1 public class MainActivity extends AppCompatActivity {
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_main);
 7 
 8         findViewById(R.id.button_show_dialog).setOnClickListener(v -> {
 9             InfoDialog infoDialog = new InfoDialog.Builder(this)
10                     .setTitle("Done")
11                     .setMessage("Something done")
12                     .setButton("OK", view ->
13                         Toast.makeText(this, "OK Clicked.", Toast.LENGTH_SHORT).show()
14                     ).create();
15             infoDialog.show();
16         });
17     }
18 }

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号