Skip to content

Commit fc256e6

Browse files
committed
新增设置按钮文字、背景颜色属性
1 parent 43dcb1d commit fc256e6

File tree

13 files changed

+181
-41
lines changed

13 files changed

+181
-41
lines changed

app/src/main/java/com/cyy/ripplebutton/App.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,25 @@ public class App extends Application {
1515
public void onCreate() {
1616
super.onCreate();
1717
RippleButton.getBuilder()
18+
// 加载中图片
1819
.setLoadingImg(R.drawable.loading)
20+
// 错误按钮显示时间
1921
.setErrorDuritaion(1000)
20-
.setRadius(4);
22+
// 默认文字颜色
23+
.setNormalTextColor(R.color.white)
24+
// 成功文字颜色
25+
.setSuccessTextColor(R.color.white)
26+
// 加载文字颜色
27+
.setLoadingTextColor(R.color.white)
28+
// 错误文字颜色
29+
.setErrorTextColor(R.color.white)
30+
// 默认背景颜色
31+
.setNormalColor(R.drawable.shape_blue)
32+
// 成功背景颜色
33+
.setSuccessColor(R.drawable.shape_green)
34+
// 加载背景颜色
35+
.setLoadingColor(R.drawable.shape_yellow)
36+
// 失敗背景颜色
37+
.setErrorColor(R.drawable.shape_red);
2138
}
2239
}

app/src/main/java/com/cyy/ripplebutton/MainActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.cyy.ripplebutton;
22

3-
import android.content.Intent;
43
import android.os.Bundle;
54
import android.support.v7.app.AppCompatActivity;
65
import android.view.View;
@@ -46,7 +45,7 @@ public void onLoadingClick() {
4645

4746
public void gray(View view) {
4847
rippleButton.showNormalButton();
49-
startActivity(new Intent(this, Main2Activity.class));
48+
// startActivity(new Intent(this, Main2Activity.class));
5049
}
5150

5251
public void green(View view) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<corners android:radius="6dp" />
5+
6+
<solid android:color="#87CEEB" />
7+
</shape>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<corners android:radius="6dp" />
5+
6+
<solid android:color="#8FBC8F"/>
7+
</shape>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<corners android:radius="6dp" />
5+
6+
<solid android:color="#FF6347"/>
7+
</shape>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
<corners android:radius="6dp" />
5+
6+
<solid android:color="#FF8C00" />
7+
</shape>

app/src/main/res/layout/activity_main.xml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@
1717
android:layout_height="wrap_content"
1818
android:layout_weight="1"
1919
android:onClick="gray"
20-
android:text="灰色" />
20+
android:text="默认" />
2121

2222
<Button
2323
android:layout_width="0dp"
2424
android:layout_height="wrap_content"
2525
android:layout_weight="1"
2626
android:onClick="green"
27-
android:text="绿色" />
27+
android:text="成功" />
2828

2929
<Button
3030
android:layout_width="0dp"
3131
android:layout_height="wrap_content"
3232
android:layout_weight="1"
3333
android:onClick="red"
34-
android:text="红色" />
34+
android:text="失败" />
3535

3636
<Button
3737
android:layout_width="0dp"
@@ -49,11 +49,10 @@
4949
android:layout_gravity="center"
5050
android:layout_marginLeft="30dp"
5151
android:layout_marginRight="30dp"
52-
app:mErrorText="红色"
52+
app:mErrorText="失败"
5353
app:mLoadingText="加载中"
54-
app:mNormalText="灰色"
55-
app:mRadius="4dp"
56-
app:mStatus="1"
57-
app:mSuccessText="绿色" />
54+
app:mNormalText="默认"
55+
app:mStatus="0"
56+
app:mSuccessText="成功" />
5857

5958
</FrameLayout>

app/src/main/res/values/colors.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,31 @@
33
<color name="colorPrimary">#3F51B5</color>
44
<color name="colorPrimaryDark">#303F9F</color>
55
<color name="colorAccent">#FF4081</color>
6+
7+
8+
<color name="black">#FF000000</color>
9+
<color name="white">#FFFFFFFF</color>
10+
<color name="green">#FF00FF00</color>
11+
<color name="transparent">#00000000</color>
12+
<color name="line">#8CD276</color>
13+
<color name="color_main">#87CD25</color>
14+
<color name="divider_color">#EBEBEB</color>
15+
<color name="color_666">#666666</color>
16+
<color name="color_333">#333333</color>
17+
<color name="color_999">#999999</color>
18+
<color name="color_f5">#F5F5F5</color>
19+
<color name="color_f1">#F1F1F1</color>
20+
<color name="DCDCDC">#DCDCDC</color>
21+
<color name="divider">#EBEBEB</color>
22+
<color name="red_end">#DD2727</color>
23+
<color name="base_text_color_light">#6FBA2C</color>
24+
<color name="alpha_75_black">#BF000000</color>
25+
26+
<color name="color_CCC">#CCCCCC</color>
27+
<color name="linechart_bottom">#000000</color>
28+
<color name="alpha_55_white">#8CFFFFFF</color>
29+
<color name="color_main_color" >#87CD25</color>
30+
31+
<color name="line_bg">#CDCDCD</color>
32+
633
</resources>

library/src/main/java/com/cyy/library/RippleButton.java

Lines changed: 89 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
import android.view.View;
1212
import android.widget.FrameLayout;
1313
import android.widget.ImageView;
14-
import android.widget.RelativeLayout;
1514
import 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); // 动画时间
File renamed without changes.

0 commit comments

Comments
 (0)