Skip to content

Commit 3764d0e

Browse files
committed
优化代码命名和注释
优化 Toast 默认圆角大小 新增支持自定义Toast显示规则 新增支持自定义Toast拦截规则 修复原生 Toast 在 Android 7.1 主线程阻塞会发生崩溃的问题
1 parent 7090639 commit 3764d0e

22 files changed

+442
-244
lines changed

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
#### 集成步骤
1414

1515
dependencies {
16-
implementation 'com.hjq:toast:6.0'
16+
implementation 'com.hjq:toast:8.0'
1717
}
1818

19-
#### 初始化Toast
19+
#### 初始化 Toast
2020

2121
// 在 Application 中初始化
2222
ToastUtils.init(this);
@@ -82,6 +82,24 @@
8282

8383
![](ToastUtils.jpg)
8484

85+
#### 如何替换项目中已有的 Toast ?
86+
87+
> 右击项目,Replace in path,勾选 Regex 选项
88+
89+
Toast\.makeText\([^,]+,\s(.+{1}),\s[^,]+\)\.show\(\);
90+
91+
> 替换使用
92+
93+
ToastUtils.show($1);
94+
95+
> 包名替换
96+
97+
import android.widget.Toast;
98+
99+
---
100+
101+
import com.hjq.toast.ToastUtils;
102+
85103
#### 作者的其他开源项目
86104

87105
* 架构工程:[AndroidProject](https://github.com/getActivity/AndroidProject)

ToastUtils.apk

190 KB
Binary file not shown.

app/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 26
4+
compileSdkVersion 28
55

66
defaultConfig {
77
applicationId "com.hjq.toast.demo"
88
minSdkVersion 14
9-
targetSdkVersion 26
10-
versionCode 60
11-
versionName "6.0"
9+
targetSdkVersion 28
10+
versionCode 80
11+
versionName "8.0"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {
@@ -22,9 +22,9 @@ android {
2222
dependencies {
2323
implementation fileTree(include: ['*.jar'], dir: 'libs')
2424
implementation project(':library')
25-
implementation 'com.android.support:appcompat-v7:26.1.0'
25+
implementation 'com.android.support:appcompat-v7:28.0.0'
2626
// 标题栏:https://github.com/getActivity/TitleBar
27-
implementation 'com.hjq:titlebar:5.0'
27+
implementation 'com.hjq:titlebar:6.0'
2828
// 悬浮窗:https://github.com/getActivity/XToast
2929
implementation 'com.hjq:xtoast:2.6'
3030
}

app/src/main/java/com/hjq/toast/demo/ToastActivity.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package com.hjq.toast.demo;
22

3-
import android.content.Intent;
43
import android.os.Bundle;
54
import android.os.Handler;
65
import android.support.v4.app.NotificationManagerCompat;
76
import android.support.v7.app.AppCompatActivity;
87
import android.view.Gravity;
98
import android.view.View;
10-
import android.widget.Toast;
119

1210
import com.hjq.toast.ToastUtils;
1311
import com.hjq.toast.style.ToastAliPayStyle;
@@ -47,22 +45,22 @@ public void run() {
4745
}
4846

4947
public void show3(View v) {
50-
ToastUtils.initStyle(new ToastWhiteStyle());
48+
ToastUtils.initStyle(new ToastWhiteStyle(getApplication()));
5149
ToastUtils.show("动态切换白色吐司样式成功");
5250
}
5351

5452
public void show4(View v) {
55-
ToastUtils.initStyle(new ToastBlackStyle());
53+
ToastUtils.initStyle(new ToastBlackStyle(getApplication()));
5654
ToastUtils.show("动态切换黑色吐司样式成功");
5755
}
5856

5957
public void show5(View v) {
60-
ToastUtils.initStyle(new ToastQQStyle());
58+
ToastUtils.initStyle(new ToastQQStyle(getApplication()));
6159
ToastUtils.show("QQ那种还不简单,分分钟的事");
6260
}
6361

6462
public void show6(View v) {
65-
ToastUtils.initStyle(new ToastAliPayStyle());
63+
ToastUtils.initStyle(new ToastAliPayStyle(getApplication()));
6664
ToastUtils.show("支付宝那种还不简单,分分钟的事");
6765
}
6866

app/src/main/java/com/hjq/toast/demo/ToastApplication.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.hjq.toast.demo;
22

33
import android.app.Application;
4+
import android.util.Log;
5+
import android.widget.Toast;
46

7+
import com.hjq.toast.ToastInterceptor;
58
import com.hjq.toast.ToastUtils;
69
import com.hjq.toast.style.ToastBlackStyle;
710

@@ -16,7 +19,20 @@ public class ToastApplication extends Application {
1619
@Override
1720
public void onCreate() {
1821
super.onCreate();
22+
// 设置 Toast 拦截器
23+
ToastUtils.setToastInterceptor(new ToastInterceptor() {
24+
@Override
25+
public boolean intercept(Toast toast, CharSequence text) {
26+
boolean intercept = super.intercept(toast, text);
27+
if (intercept) {
28+
Log.e("Toast", "空 Toast");
29+
} else {
30+
Log.i("Toast", text.toString());
31+
}
32+
return intercept;
33+
}
34+
});
1935
// 初始化吐司工具类
20-
ToastUtils.init(this, new ToastBlackStyle());
36+
ToastUtils.init(this, new ToastBlackStyle(this));
2137
}
2238
}

library/build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ android {
1010
defaultConfig {
1111
minSdkVersion 3
1212
targetSdkVersion 26
13-
versionCode 60
14-
versionName "6.0"
13+
versionCode 80
14+
versionName "8.0"
15+
1516
}
1617
}
1718

1819
publish {
1920
userOrg = 'getactivity'//填写bintray用户名,注意大小写
2021
groupId = 'com.hjq'//定义的maven group id最终引用形式
2122
artifactId = 'toast'//maven的artifact id
22-
version = '6.0'//maven 上发布版本号
23+
version = '8.0'//maven 上发布版本号
2324
description = 'This is a very functional Toast'//描述,自己定义
2425
website = "https://github.com/getActivity/ToastUtils"//项目在github中的地址
2526
}

library/src/main/java/com/hjq/toast/BaseToast.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
*/
1515
public class BaseToast extends Toast {
1616

17-
// 吐司消息 View
17+
/** 吐司消息 View */
1818
private TextView mMessageView;
1919

20-
BaseToast(Application application) {
20+
public BaseToast(Application application) {
2121
super(application);
2222
}
2323

@@ -60,7 +60,9 @@ private static TextView findTextView(ViewGroup group) {
6060
return (TextView) view;
6161
} else if (view instanceof ViewGroup) {
6262
TextView textView = findTextView((ViewGroup) view);
63-
if (textView != null) return textView;
63+
if (textView != null) {
64+
return textView;
65+
}
6466
}
6567
}
6668
return null;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.hjq.toast;
2+
3+
import android.widget.Toast;
4+
5+
/**
6+
* author : Android 轮子哥
7+
* github : https://github.com/getActivity/ToastUtils
8+
* time : 2019/05/19
9+
* desc : Toast 拦截器接口
10+
*/
11+
public interface IToastInterceptor {
12+
13+
/**
14+
* 根据显示的文本决定是否拦截该 Toast
15+
*/
16+
boolean intercept(Toast toast, CharSequence text);
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.hjq.toast;
2+
3+
import android.widget.Toast;
4+
5+
/**
6+
* author : Android 轮子哥
7+
* github : https://github.com/getActivity/ToastUtils
8+
* time : 2019/05/19
9+
* desc : Toast 处理策略
10+
*/
11+
public interface IToastStrategy {
12+
13+
/** 短吐司显示的时长 */
14+
int SHORT_DURATION_TIMEOUT = 2000;
15+
/** 长吐司显示的时长 */
16+
int LONG_DURATION_TIMEOUT = 3500;
17+
18+
/**
19+
* 绑定 Toast 对象
20+
*/
21+
void bind(Toast toast);
22+
23+
/**
24+
* 显示 Toast
25+
*/
26+
void show(CharSequence text);
27+
28+
/**
29+
* 取消 Toast
30+
*/
31+
void cancel();
32+
}

library/src/main/java/com/hjq/toast/IToastStyle.java

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,68 @@
88
*/
99
public interface IToastStyle {
1010

11-
int getGravity(); // 吐司的重心
12-
int getXOffset(); // X轴偏移
13-
int getYOffset(); // Y轴偏移
14-
int getZ(); // 吐司Z轴坐标
15-
16-
int getCornerRadius(); // 圆角大小
17-
int getBackgroundColor(); // 背景颜色
18-
19-
int getTextColor(); // 文本颜色
20-
float getTextSize(); // 文本大小
21-
int getMaxLines(); // 最大行数
22-
23-
int getPaddingLeft(); // 左边内边距
24-
int getPaddingTop(); // 顶部内边距
25-
int getPaddingRight(); // 右边内边距
26-
int getPaddingBottom(); // 底部内边距
11+
/**
12+
* 吐司的重心
13+
*/
14+
int getGravity();
15+
16+
/**
17+
* X轴偏移
18+
*/
19+
int getXOffset();
20+
21+
/**
22+
* Y轴偏移
23+
*/
24+
int getYOffset();
25+
26+
/**
27+
* 吐司 Z 轴坐标
28+
*/
29+
int getZ();
30+
31+
/**
32+
* 圆角大小
33+
*/
34+
int getCornerRadius();
35+
36+
/**
37+
* 背景颜色
38+
*/
39+
int getBackgroundColor();
40+
41+
/**
42+
* 文本颜色
43+
*/
44+
int getTextColor();
45+
46+
/**
47+
* 文本大小
48+
*/
49+
float getTextSize();
50+
51+
/**
52+
* 最大行数
53+
*/
54+
int getMaxLines();
55+
56+
/**
57+
* 开始内边距
58+
*/
59+
int getPaddingStart();
60+
61+
/**
62+
* 顶部内边距
63+
*/
64+
int getPaddingTop();
65+
66+
/**
67+
* 结束内边距
68+
*/
69+
int getPaddingEnd();
70+
71+
/**
72+
* 底部内边距
73+
*/
74+
int getPaddingBottom();
2775
}

0 commit comments

Comments
 (0)