Skip to content

Commit 542c371

Browse files
committed
新增在策略计算 Toast 显示时长方法
优化 isSupportToastStyle 方法命名
1 parent acf4a60 commit 542c371

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,6 @@ public ToastStrategy() {
8181
this(ToastStrategy.SHOW_STRATEGY_TYPE_IMMEDIATELY);
8282
}
8383

84-
@Override
85-
public void registerStrategy(Application application) {
86-
mApplication = application;
87-
}
88-
8984
public ToastStrategy(int type) {
9085
mShowStrategyType = type;
9186
switch (mShowStrategyType) {
@@ -97,6 +92,16 @@ public ToastStrategy(int type) {
9792
}
9893
}
9994

95+
@Override
96+
public void registerStrategy(Application application) {
97+
mApplication = application;
98+
}
99+
100+
@Override
101+
public int computeShowDuration(CharSequence text) {
102+
return text.length() > 20 ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
103+
}
104+
100105
@Override
101106
public IToast createToast(ToastParams params) {
102107
Activity foregroundActivity = getForegroundActivity();
@@ -123,7 +128,7 @@ public IToast createToast(ToastParams params) {
123128
} else {
124129
toast = new SystemToast(mApplication);
125130
}
126-
if (isSupportToastStyle(toast) || !onlyShowSystemToastStyle()) {
131+
if (areSupportCustomToastStyle(toast) || !onlyShowSystemToastStyle()) {
127132
diyToastStyle(toast, params.style);
128133
}
129134
return toast;
@@ -168,7 +173,7 @@ public void cancelToast() {
168173
/**
169174
* 是否支持设置自定义 Toast 样式
170175
*/
171-
protected boolean isSupportToastStyle(IToast toast) {
176+
protected boolean areSupportCustomToastStyle(IToast toast) {
172177
// targetSdkVersion >= 30 的情况下在后台显示自定义样式的 Toast 会被系统屏蔽,并且日志会输出以下警告:
173178
// Blocking custom toast from package com.xxx.xxx due to package not in the foreground
174179
// targetSdkVersion < 30 的情况下 new Toast,并且不设置视图显示,系统会抛出以下异常:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public static void show(ToastParams params) {
218218
}
219219

220220
if (params.duration == -1) {
221-
params.duration = params.text.length() > 20 ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
221+
params.duration = params.strategy.computeShowDuration(params.text);
222222
}
223223

224224
params.strategy.showToast(params);

library/src/main/java/com/hjq/toast/config/IToastStrategy.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ public interface IToastStrategy {
1616
*/
1717
void registerStrategy(Application application);
1818

19+
/**
20+
* 计算 Toast 显示时长
21+
*/
22+
int computeShowDuration(CharSequence text);
23+
1924
/**
2025
* 创建 Toast
2126
*/

0 commit comments

Comments
 (0)