经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android自定义弹出框的方法
来源:jb51  时间:2022/6/21 16:40:45  对本文有异议

在开发Android项目的过程中,弹出框真的是我们的常见的一种互动式窗体,但是Android系统自带的弹出框往往都不能满足我们的需要,大多数的时候需要我们自定义一种更漂亮的窗体来来展示给用户。

接下来是我很久之前用的一个自定义弹出框,记录一下,以便自己日后使用。

0、先来一张效果图

1、先定义个一个继承自Dialog的自定义弹框CustomDialog

  1. import android.app.Dialog;
  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.Button;
  7. import android.widget.TextView;
  8. ?
  9. /**
  10. ?* 作者 Aaron Zhao?
  11. ?* 时间 2015/9/16 11:21?
  12. ?* 名称 CustomDialog.java 描述
  13. ?*/
  14. public class CustomDialog extends Dialog {
  15. ?? ?/* Constructor */
  16. ?? ?private CustomDialog(Context context) {
  17. ?? ??? ?super(context);
  18. ?? ?}
  19. ?
  20. ?? ?private CustomDialog(Context context, int themeResId) {
  21. ?? ??? ?super(context, themeResId);
  22. ?? ?}
  23. ?
  24. ?? ?private CustomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
  25. ?? ??? ?super(context, cancelable, cancelListener);
  26. ?? ?}
  27. ?
  28. ?? ?/* Builder */
  29. ?? ?public static class Builder {
  30. ?? ??? ?private TextView tvTitle, tvWarning, tvInfo;
  31. ?? ??? ?private Button btnCancel, btnConfirm;
  32. ?
  33. ?? ??? ?private View mLayout;
  34. ?? ??? ?private View.OnClickListener mButtonCancelClickListener;
  35. ?? ??? ?private View.OnClickListener mButtonConfirmClickListener;
  36. ?
  37. ?? ??? ?private CustomDialog mDialog;
  38. ?
  39. ?? ??? ?public Builder(Context context) {
  40. ?? ??? ??? ?mDialog = new CustomDialog(context, R.style.custom_dialog);
  41. ?? ??? ??? ?LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  42. ?? ??? ??? ?// 加载布局文件
  43. ?? ??? ??? ?mLayout = inflater.inflate(R.layout.dialog_custom, null, false);
  44. ?? ??? ??? ?// 添加布局文件到 Dialog
  45. ?? ??? ??? ?mDialog.addContentView(mLayout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
  46. ?
  47. ?? ??? ??? ?tvTitle = (TextView) mLayout.findViewById(R.id.tv_title);
  48. ?? ??? ??? ?tvWarning = (TextView) mLayout.findViewById(R.id.tv_warning);
  49. ?? ??? ??? ?tvInfo = (TextView) mLayout.findViewById(R.id.tv_info);
  50. ?? ??? ??? ?btnCancel = (Button) mLayout.findViewById(R.id.btn_cancel);
  51. ?? ??? ??? ?btnConfirm = (Button) mLayout.findViewById(R.id.btn_confirm);
  52. ?? ??? ?}
  53. ?
  54. ?? ??? ?/**
  55. ?? ??? ? * 设置 Dialog 标题
  56. ?? ??? ? */
  57. ?? ??? ?public Builder setTitle(String title) {
  58. ?? ??? ??? ?tvTitle.setText(title);
  59. ?? ??? ??? ?tvTitle.setVisibility(View.VISIBLE);
  60. ?? ??? ??? ?return this;
  61. ?? ??? ?}
  62. ?
  63. ?? ??? ?/**
  64. ?? ??? ? * 设置 Warning
  65. ?? ??? ? */
  66. ?? ??? ?public Builder setWarning(String waring) {
  67. ?? ??? ??? ?tvWarning.setText(waring);
  68. ?? ??? ??? ?if (waring == null || waring.equals("")) {
  69. ?? ??? ??? ??? ?tvWarning.setVisibility(View.GONE);
  70. ?? ??? ??? ?}
  71. ?? ??? ??? ?return this;
  72. ?? ??? ?}
  73. ?
  74. ?? ??? ?/**
  75. ?? ??? ? * 设置 Info
  76. ?? ??? ? */
  77. ?? ??? ?public Builder setInfo(String message) {
  78. ?? ??? ??? ?tvInfo.setText(message);
  79. ?? ??? ??? ?return this;
  80. ?? ??? ?}
  81. ?
  82. ?? ??? ?/**
  83. ?? ??? ? * 设置取消按钮文字和监听
  84. ?? ??? ? */
  85. ?? ??? ?public Builder setButtonCancel(String text, View.OnClickListener listener) {
  86. ?? ??? ??? ?btnCancel.setText(text);
  87. ?? ??? ??? ?mButtonCancelClickListener = listener;
  88. ?? ??? ??? ?return this;
  89. ?? ??? ?}
  90. ?
  91. ?? ??? ?/**
  92. ?? ??? ? * 设置确认按钮文字和监听
  93. ?? ??? ? */
  94. ?? ??? ?public Builder setButtonConfirm(String text, View.OnClickListener listener) {
  95. ?? ??? ??? ?btnConfirm.setText(text);
  96. ?? ??? ??? ?mButtonConfirmClickListener = listener;
  97. ?? ??? ??? ?return this;
  98. ?? ??? ?}
  99. ?
  100. ?? ??? ?public CustomDialog create() {
  101. ?? ??? ??? ?btnCancel.setOnClickListener(new android.view.View.OnClickListener() {
  102. ?? ??? ??? ??? ?@Override
  103. ?? ??? ??? ??? ?public void onClick(View view) {
  104. ?? ??? ??? ??? ??? ?mDialog.dismiss();
  105. ?? ??? ??? ??? ??? ?mButtonCancelClickListener.onClick(view);
  106. ?? ??? ??? ??? ?}
  107. ?? ??? ??? ?});
  108. ?
  109. ?? ??? ??? ?btnConfirm.setOnClickListener(new android.view.View.OnClickListener() {
  110. ?? ??? ??? ??? ?@Override
  111. ?? ??? ??? ??? ?public void onClick(View view) {
  112. ?? ??? ??? ??? ??? ?mDialog.dismiss();
  113. ?? ??? ??? ??? ??? ?mButtonConfirmClickListener.onClick(view);
  114. ?? ??? ??? ??? ?}
  115. ?? ??? ??? ?});
  116. ?
  117. ?? ??? ??? ?mDialog.setContentView(mLayout);
  118. ?? ??? ??? ?mDialog.setCancelable(true);?
  119. ?? ??? ??? ?mDialog.setCanceledOnTouchOutside(false);?
  120. ?? ??? ??? ?return mDialog;
  121. ?? ??? ?}
  122. ?? ?}
  123. }

2、定义CustomDialo布局文件:dialog_custom

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. ? ? android:layout_width="match_parent"
  4. ? ? android:layout_height="match_parent"
  5. ? ? android:gravity="center"
  6. ? ? android:orientation="vertical">
  7. ?
  8. ? ? <LinearLayout
  9. ? ? ? ? android:layout_width="300dp"
  10. ? ? ? ? android:layout_height="wrap_content"
  11. ? ? ? ? android:background="@drawable/rect_s_rounded_white"
  12. ? ? ? ? android:orientation="vertical">
  13. ?
  14. ? ? ? ? <LinearLayout
  15. ? ? ? ? ? ? android:layout_width="match_parent"
  16. ? ? ? ? ? ? android:layout_height="wrap_content"
  17. ? ? ? ? ? ? android:layout_marginTop="@dimen/margin_10"
  18. ? ? ? ? ? ? android:layout_marginBottom="@dimen/margin_10"
  19. ? ? ? ? ? ? android:gravity="center"
  20. ? ? ? ? ? ? android:orientation="vertical">
  21. ?
  22. ? ? ? ? ? ? <TextView
  23. ? ? ? ? ? ? ? ? android:id="@+id/tv_title"
  24. ? ? ? ? ? ? ? ? android:layout_width="wrap_content"
  25. ? ? ? ? ? ? ? ? android:layout_height="wrap_content"
  26. ? ? ? ? ? ? ? ? android:textSize="@dimen/size_18"
  27. ? ? ? ? ? ? ? ? android:textStyle="bold" />
  28. ? ? ? ? </LinearLayout>
  29. ?
  30. ? ? ? ? <LinearLayout
  31. ? ? ? ? ? ? android:layout_width="match_parent"
  32. ? ? ? ? ? ? android:layout_height="wrap_content"
  33. ? ? ? ? ? ? android:orientation="vertical"
  34. ? ? ? ? ? ? android:padding="@dimen/padding_10">
  35. ?
  36. ? ? ? ? ? ? <TextView
  37. ? ? ? ? ? ? ? ? android:id="@+id/tv_warning"
  38. ? ? ? ? ? ? ? ? android:layout_width="match_parent"
  39. ? ? ? ? ? ? ? ? android:layout_height="wrap_content"
  40. ? ? ? ? ? ? ? ? android:textColor="@android:color/holo_red_dark"
  41. ? ? ? ? ? ? ? ? android:textSize="@dimen/size_14" />
  42. ?
  43. ? ? ? ? ? ? <TextView
  44. ? ? ? ? ? ? ? ? android:id="@+id/tv_info"
  45. ? ? ? ? ? ? ? ? android:layout_width="match_parent"
  46. ? ? ? ? ? ? ? ? android:layout_height="wrap_content"
  47. ? ? ? ? ? ? ? ? android:layout_marginTop="@dimen/margin_10"
  48. ? ? ? ? ? ? ? ? android:textColor="@android:color/black"
  49. ? ? ? ? ? ? ? ? android:textSize="@dimen/size_14" />
  50. ? ? ? ? </LinearLayout>
  51. ?
  52. ? ? ? ? <LinearLayout
  53. ? ? ? ? ? ? android:layout_width="match_parent"
  54. ? ? ? ? ? ? android:layout_height="wrap_content"
  55. ? ? ? ? ? ? android:layout_marginTop="@dimen/margin_10"
  56. ? ? ? ? ? ? android:layout_marginBottom="@dimen/margin_10"
  57. ? ? ? ? ? ? android:gravity="center"
  58. ? ? ? ? ? ? android:orientation="horizontal"
  59. ? ? ? ? ? ? android:padding="@dimen/padding_10">
  60. ?
  61. ? ? ? ? ? ? <Button
  62. ? ? ? ? ? ? ? ? android:id="@+id/btn_cancel"
  63. ? ? ? ? ? ? ? ? android:layout_width="110dp"
  64. ? ? ? ? ? ? ? ? android:layout_height="40dp"
  65. ? ? ? ? ? ? ? ? android:layout_marginRight="25dp"
  66. ? ? ? ? ? ? ? ? android:background="@drawable/button_gray_l_rounded_selector"
  67. ? ? ? ? ? ? ? ? android:textColor="@android:color/black"
  68. ? ? ? ? ? ? ? ? android:textSize="@dimen/size_14" />
  69. ?
  70. ? ? ? ? ? ? <Button
  71. ? ? ? ? ? ? ? ? android:id="@+id/btn_confirm"
  72. ? ? ? ? ? ? ? ? android:layout_width="110dp"
  73. ? ? ? ? ? ? ? ? android:layout_height="40dp"
  74. ? ? ? ? ? ? ? ? android:background="@drawable/button_green_l_rounded_selector"
  75. ? ? ? ? ? ? ? ? android:textColor="@android:color/white"
  76. ? ? ? ? ? ? ? ? android:textSize="@dimen/size_14" />
  77. ? ? ? ? </LinearLayout>
  78. ? ? </LinearLayout>
  79. ?
  80. </LinearLayout>

3、定义CustomDialog样式文件:custom_dialog

  1. <style name="custom_dialog" parent="@android:style/Theme.Dialog">
  2. ? ? ? ? <item name="android:windowFrame">@null</item>
  3. ? ? ? ? <item name="android:windowIsFloating">true</item>
  4. ? ? ? ? <item name="android:windowIsTranslucent">true</item>
  5. ? ? ? ? <item name="android:windowNoTitle">true</item>
  6. ? ? ? ? <item name="android:background">#00000000</item>
  7. ? ? ? ? <item name="android:windowBackground">@color/transparent</item>
  8. ? ? ? ? <item name="android:backgroundDimEnabled">true</item>
  9. </style>

4、定义其中按钮样式:button_green_l_rounded_selector和button_gray_l_rounded_selector

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. ?
  4. ? ? <item android:state_pressed="true">
  5. ? ? ? ? <shape android:shape="rectangle">
  6. ? ? ? ? ? ? <stroke android:color="@android:color/transparent" android:width="0dp"></stroke>
  7. ?
  8. ? ? ? ? ? ? <gradient android:angle="180"
  9. ? ? ? ? ? ? ? ? android:endColor="@color/button_green"
  10. ? ? ? ? ? ? ? ? android:startColor="@color/button_green"></gradient>
  11. ?
  12. ? ? ? ? ? ? <corners android:radius="50dp"></corners>
  13. ? ? ? ? </shape>
  14. ? ? </item>
  15. ?
  16. ? ? <item android:state_focused="true">
  17. ? ? ? ? <shape android:shape="rectangle">
  18. ? ? ? ? ? ? <stroke android:color="@android:color/transparent" android:width="0dp"></stroke>
  19. ?
  20. ? ? ? ? ? ? <gradient android:angle="180"
  21. ? ? ? ? ? ? ? ? android:endColor="@color/button_green"
  22. ? ? ? ? ? ? ? ? android:startColor="@color/button_green"></gradient>
  23. ?
  24. ? ? ? ? ? ? <corners android:radius="50dp"></corners>
  25. ? ? ? ? </shape>
  26. ? ? </item>
  27. ?
  28. ? ? <item android:state_pressed="false">
  29. ? ? ? ? <shape android:shape="rectangle">
  30. ? ? ? ? ? ? <stroke android:color="@android:color/transparent" android:width="0dp"></stroke>
  31. ?
  32. ? ? ? ? ? ? <gradient android:angle="0"
  33. ? ? ? ? ? ? ? ? android:endColor="@color/button_green"
  34. ? ? ? ? ? ? ? ? android:startColor="@color/button_green"></gradient>
  35. ?
  36. ? ? ? ? ? ? <corners android:radius="50dp"></corners>
  37. ? ? ? ? </shape>
  38. ? ? </item>
  39. ?
  40. ? ? <item android:state_focused="false">
  41. ? ? ? ? <shape android:shape="rectangle">
  42. ? ? ? ? ? ? <stroke android:color="@android:color/transparent" android:width="0dp"></stroke>
  43. ?
  44. ? ? ? ? ? ? <gradient android:angle="0"
  45. ? ? ? ? ? ? ? ? android:endColor="@color/button_green"
  46. ? ? ? ? ? ? ? ? android:startColor="@color/button_green"></gradient>
  47. ?
  48. ? ? ? ? ? ? <corners android:radius="50dp"></corners>
  49. ? ? ? ? </shape>
  50. ? ? </item>
  51. ?
  52. </selector>

5、具体使用

  1. private void showDialog() {
  2. ?? ??? ?OnClickListener onCancelClickListener = new OnClickListener() {?? ??? ??? ?
  3. ?? ??? ??? ?@Override
  4. ?? ??? ??? ?public void onClick(View v) {
  5. ?? ??? ??? ??? ?MyDialog.showToast(WarehouseAddActivity.this, "取消");
  6. ?? ??? ??? ?}
  7. ?? ??? ?};
  8. ?? ??? ?
  9. ?? ??? ?OnClickListener onConfimClickListener = new OnClickListener() {?? ??? ??? ?
  10. ?? ??? ??? ?@Override
  11. ?? ??? ??? ?public void onClick(View v) {
  12. ?? ??? ??? ??? ?MyDialog.showToast(WarehouseAddActivity.this, "确认");
  13. ?? ??? ??? ?}
  14. ?? ??? ?};
  15. ?? ??? ?
  16. ?? ??? ?showInfoDialog("", "请点击确认进行操作", "取消", onCancelClickListener, "确认", onConfimClickListener);
  17. ?? ?}
  18. ?
  19. protected void showInfoDialog(String waring, String info, String cancelText, OnClickListener cancelOnClick, String confirmText,
  20. ?? ??? ??? ?OnClickListener confirmOnClick) {
  21. ?? ??? ?CustomDialog.Builder builder = new CustomDialog.Builder(this);
  22. ?? ??? ?builder.setTitle("提示");
  23. ?? ??? ?builder.setWarning(waring);
  24. ?? ??? ?builder.setInfo(info);
  25. ?? ??? ?builder.setButtonCancel(cancelText, cancelOnClick);
  26. ?? ??? ?builder.setButtonConfirm(confirmText, confirmOnClick);
  27. ?
  28. ?? ??? ?CustomDialog customDialog = builder.create();
  29. ?? ??? ?customDialog.show();
  30. ?? ?}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

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

本站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号