1111import android .view .View ;
1212import android .widget .FrameLayout ;
1313import android .widget .ImageView ;
14- import android .widget .RelativeLayout ;
1514import android .widget .TextView ;
1615
1716
18- public class RippleButton extends RelativeLayout implements View .OnClickListener {
17+ public class RippleButton extends FrameLayout implements View .OnClickListener {
1918 public static final int NORMAL = 0 ;
2019 public static final int SUCCESS = 1 ;
2120 public static final int ERROR = 2 ;
@@ -72,8 +71,37 @@ public class RippleButton extends RelativeLayout implements View.OnClickListener
7271 * 涟漪动画时间
7372 */
7473 private long perfectMills = 300 ;
74+ /**
75+ * 公共属性
76+ */
7577 private static Builder builder = new Builder ();
7678
79+
80+ @ ColorRes
81+ private int mNormalTextColor ;
82+ @ ColorRes
83+ private int mErrorTextColor ;
84+ @ ColorRes
85+ private int mSuccessTextColor ;
86+ @ ColorRes
87+ private int mLoadingTextColor ;
88+ /**
89+ * 默认按钮颜色
90+ */
91+ private int mNormalColor ;
92+ /**
93+ * 错误按钮颜色
94+ */
95+ private int mErrorColor ;
96+ /**
97+ * 成功按钮颜色
98+ */
99+ private int mSuccessColor ;
100+ /**
101+ * 加载按钮颜色
102+ */
103+ private int mLoadingColor ;
104+
77105 public RippleButton (Context context ) {
78106 this (context , null );
79107 }
@@ -110,9 +138,9 @@ private void setUp(AttributeSet attrs) {
110138 } finally {
111139 a .recycle ();
112140 }
113- mTvNormal = findViewById (R .id .tv_gray );
114- mTvError = findViewById (R .id .tv_red );
115- mTvSuccess = findViewById (R .id .tv_green );
141+ mTvNormal = findViewById (R .id .tv_normal );
142+ mTvError = findViewById (R .id .tv_error );
143+ mTvSuccess = findViewById (R .id .tv_success );
116144 mFlLoading = findViewById (R .id .fl_loading );
117145 mTvLoading = findViewById (R .id .tv_loading );
118146 mIvLoading = findViewById (R .id .iv_loading );
@@ -121,10 +149,18 @@ private void setUp(AttributeSet attrs) {
121149 // 成功按钮 默认显示 默认 按钮的字
122150 mTvSuccess .setText (TextUtils .isEmpty (mSuccessText ) ? mNormalText : mSuccessText );
123151 mTvLoading .setText (TextUtils .isEmpty (mLoadingText ) ? mContext .getString (R .string .loading ) : mLoadingText );
124- mTvNormal .setTextColor (getResources ().getColor (R .color .color_999 ));
125- mTvError .setTextColor (getResources ().getColor (R .color .white ));
126- mTvSuccess .setTextColor (getResources ().getColor (R .color .white ));
152+ // 文字颜色
153+ mTvNormal .setTextColor (getResources ().getColor (mNormalTextColor ));
154+ mTvError .setTextColor (getResources ().getColor (mErrorTextColor ));
155+ mTvSuccess .setTextColor (getResources ().getColor (mSuccessTextColor ));
156+ mTvLoading .setTextColor (getResources ().getColor (mLoadingTextColor ));
127157 mIvLoading .setImageResource (mLoadingImg );
158+ // 按钮背景颜色
159+ mTvNormal .setBackgroundResource (mNormalColor );
160+ mTvSuccess .setBackgroundResource (mSuccessColor );
161+ mTvError .setBackgroundResource (mErrorColor );
162+ mFlLoading .setBackgroundResource (mLoadingColor );
163+ // 按钮点击事件
128164 mTvNormal .setOnClickListener (this );
129165 mTvSuccess .setOnClickListener (this );
130166 mTvError .setOnClickListener (this );
@@ -152,6 +188,17 @@ private void setBuilder() {
152188 this .mLoadingImg = builder .mLoadingImg ;
153189 this .mRadius = builder .mRadius ;
154190 this .duration = builder .mErrorDuritaion ;
191+ // 按钮文字颜色
192+ this .mNormalTextColor = builder .mNormalTextColor ;
193+ this .mSuccessTextColor = builder .mSuccessTextColor ;
194+ this .mLoadingTextColor = builder .mLoadingTextColor ;
195+ this .mErrorTextColor = builder .mErrorTextColor ;
196+ // 按钮背景颜色,通过shape控制颜色、圆角等
197+ this .mNormalColor = builder .mNormalColor ;
198+ this .mErrorColor = builder .mErrorColor ;
199+ this .mSuccessColor = builder .mSuccessColor ;
200+ this .mLoadingColor = builder .mLoadingColor ;
201+
155202 }
156203
157204 /**
@@ -358,11 +405,11 @@ public RippleButton setErrorDuration(long duration) {
358405 */
359406 @ Override
360407 public void onClick (View view ) {
361- if (view .getId () == R .id .tv_gray ) {// 灰色
408+ if (view .getId () == R .id .tv_normal ) {// 灰色
362409 if (listener != null ) listener .onDefaultClick ();
363- } else if (view .getId () == R .id .tv_green ) { // 绿色
410+ } else if (view .getId () == R .id .tv_success ) { // 绿色
364411 if (listener != null ) listener .onSuccessClick ();
365- } else if (view .getId () == R .id .tv_red ) { // 红色
412+ } else if (view .getId () == R .id .tv_error ) { // 红色
366413 if (listener != null ) listener .onErrorClick ();
367414 } else if (view .getId () == R .id .fl_loading ) { // 加载
368415 if (listener != null ) listener .onLoadingClick ();
@@ -397,23 +444,27 @@ public static class Builder {
397444 /**
398445 * 默认按钮颜色
399446 */
400- @ ColorRes
401- private int mNormalColor ;
447+ private int mNormalColor = R .drawable .shape_normal ;
402448 /**
403449 * 错误按钮颜色
404450 */
405- @ ColorRes
406- private int mErrorColor ;
451+ private int mErrorColor = R .drawable .shape_error ;
407452 /**
408453 * 成功按钮颜色
409454 */
410- @ ColorRes
411- private int mSuccessColor ;
455+ private int mSuccessColor = R .drawable .shape_success ;
412456 /**
413457 * 加载按钮颜色
414458 */
459+ private int mLoadingColor = R .drawable .shape_success ;
415460 @ ColorRes
416- private int mLoadingColor ;
461+ private int mNormalTextColor = R .color .color_999 ;
462+ @ ColorRes
463+ private int mErrorTextColor = R .color .white ;
464+ @ ColorRes
465+ private int mSuccessTextColor = R .color .white ;
466+ @ ColorRes
467+ private int mLoadingTextColor = R .color .white ;
417468
418469 public Builder setErrorDuritaion (int mErrorDuritaion ) {
419470 this .mErrorDuritaion = mErrorDuritaion ;
@@ -450,6 +501,26 @@ public Builder setLoadingColor(int mLoadingColor) {
450501 return this ;
451502 }
452503
504+ public Builder setNormalTextColor (int mNormalTextColor ) {
505+ this .mNormalTextColor = mNormalTextColor ;
506+ return this ;
507+ }
508+
509+ public Builder setErrorTextColor (int mErrorTextColor ) {
510+ this .mErrorTextColor = mErrorTextColor ;
511+ return this ;
512+ }
513+
514+ public Builder setSuccessTextColor (int mSuccessTextColor ) {
515+ this .mSuccessTextColor = mSuccessTextColor ;
516+ return this ;
517+ }
518+
519+ public Builder setLoadingTextColor (int mLoadingTextColor ) {
520+ this .mLoadingTextColor = mLoadingTextColor ;
521+ return this ;
522+ }
523+
453524 public void setPerfectMills (long perfectMills ) {
454525 this .perfectMills = perfectMills ;
455526 CircularAnim .init (perfectMills , 0 , Color .WHITE ); // 动画时间
0 commit comments