Skip to content

Commit a25109a

Browse files
committed
新增搭配XToast案例,优化Toast显示延迟
1 parent 39b1770 commit a25109a

File tree

9 files changed

+52
-21
lines changed

9 files changed

+52
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#### 集成步骤
1414

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

1919
#### 初始化Toast

ToastUtils.apk

123 KB
Binary file not shown.

app/build.gradle

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

33
android {
44
compileSdkVersion 26
5-
buildToolsVersion "27.0.3"
5+
66
defaultConfig {
77
applicationId "com.hjq.toast.demo"
88
minSdkVersion 14
99
targetSdkVersion 26
10-
versionCode 56
11-
versionName "5.6"
10+
versionCode 58
11+
versionName "5.8"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {
@@ -23,5 +23,8 @@ dependencies {
2323
implementation fileTree(include: ['*.jar'], dir: 'libs')
2424
implementation project(':library')
2525
implementation 'com.android.support:appcompat-v7:26.1.0'
26+
// 标题栏:https://github.com/getActivity/TitleBar
2627
implementation 'com.hjq:titlebar:5.0'
28+
// 悬浮窗:https://github.com/getActivity/XToast
29+
implementation 'com.hjq:xtoast:2.0'
2730
}

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

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

33
import android.os.Bundle;
4+
import android.os.Handler;
45
import android.support.v4.app.NotificationManagerCompat;
56
import android.support.v7.app.AppCompatActivity;
67
import android.view.Gravity;
@@ -11,6 +12,7 @@
1112
import com.hjq.toast.style.ToastBlackStyle;
1213
import com.hjq.toast.style.ToastQQStyle;
1314
import com.hjq.toast.style.ToastWhiteStyle;
15+
import com.hjq.xtoast.XToast;
1416

1517
/**
1618
* author : Android 轮子哥
@@ -69,21 +71,36 @@ public void show7(View v) {
6971
ToastUtils.show("我是自定义Toast");
7072
}
7173

74+
public void show8(View v) {
75+
new XToast(ToastActivity.this)
76+
.setDuration(1000)
77+
.setView(ToastUtils.getToast().getView())
78+
.setAnimStyle(android.R.style.Animation_Translucent)
79+
.setText(android.R.id.message, "就问你溜不溜")
80+
.show();
81+
}
82+
7283
@Override
7384
protected void onRestart() {
7485
super.onRestart();
86+
// 请注意这段代码强烈建议不要放到实际开发中,因为用户屏蔽通知栏和开启应用状态下的情况极少,可以忽略不计
87+
7588
// 如果通知栏的权限被手动关闭了
7689
if (!NotificationManagerCompat.from(this).areNotificationsEnabled() &&
7790
!"SupportToast".equals(ToastUtils.getToast().getClass().getSimpleName())) {
78-
// 因为吐司只有初始化的时候才会判断通知权限有没有开启,根据这个通知开关来显示原生的吐司还是兼容的吐司
79-
ToastUtils.init(getApplication());
80-
recreate();
81-
getWindow().getDecorView().postDelayed(new Runnable() {
82-
@Override
83-
public void run() {
84-
ToastUtils.show("检查到你手动关闭了通知权限,正在重新初始化ToastUtils框架");
85-
}
86-
}, 1000);
91+
try {
92+
// 因为吐司只有初始化的时候才会判断通知权限有没有开启,根据这个通知开关来显示原生的吐司还是兼容的吐司
93+
ToastUtils.init(getApplication());
94+
recreate();
95+
new Handler().postDelayed(new Runnable() {
96+
@Override
97+
public void run() {
98+
try {
99+
ToastUtils.show("检查到你手动关闭了通知权限,正在重新初始化ToastUtils框架");
100+
} catch (Exception ignored) {}
101+
}
102+
}, 2000);
103+
}catch (Exception ignored) {}
87104
}
88105
}
89106
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
app:backButton="false"
1515
app:title="https://github.com/getActivity/ToastUtils" />
1616

17-
<FrameLayout
17+
<ScrollView
1818
android:layout_width="match_parent"
1919
android:layout_height="match_parent">
2020

@@ -28,7 +28,6 @@
2828
android:layout_width="wrap_content"
2929
android:layout_height="wrap_content"
3030
android:layout_gravity="center_horizontal"
31-
android:layout_marginTop="30dp"
3231
android:onClick="show1"
3332
android:text="连续显示三个不同吐司" />
3433

@@ -80,8 +79,16 @@
8079
android:onClick="show7"
8180
android:text="点我自定义Toast布局" />
8281

82+
<Button
83+
android:layout_width="wrap_content"
84+
android:layout_height="wrap_content"
85+
android:layout_gravity="center_horizontal"
86+
android:layout_marginTop="30dp"
87+
android:onClick="show8"
88+
android:text="搭配XToast悬浮窗框架" />
89+
8390
</LinearLayout>
8491

85-
</FrameLayout>
92+
</ScrollView>
8693

8794
</LinearLayout>

app/src/main/res/values-v21/styles.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
88
<item name="colorAccent">@color/colorAccent</item>
99
<item name="android:navigationBarColor">@color/colorPrimary</item>
10+
<!-- 解决 Android 5.1 及以上版本 Button 英文字符串自动变大写的问题 -->
11+
<item name="android:textAllCaps">false</item>
1012
</style>
1113

1214
</resources>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<item name="colorPrimary">@color/colorPrimary</item>
77
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
88
<item name="colorAccent">@color/colorAccent</item>
9+
<!-- 解决 Android 5.1 及以上版本 Button 英文字符串自动变大写的问题 -->
10+
<item name="android:textAllCaps">false</item>
911
</style>
1012

1113
</resources>

library/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ android {
1010
defaultConfig {
1111
minSdkVersion 3
1212
targetSdkVersion 26
13-
versionCode 56
14-
versionName "5.6"
13+
versionCode 58
14+
versionName "5.8"
1515
}
1616
}
1717

1818
publish {
1919
userOrg = 'getactivity'//填写bintray用户名,注意大小写
2020
groupId = 'com.hjq'//定义的maven group id最终引用形式
2121
artifactId = 'toast'//maven的artifact id
22-
version = '5.6'//maven 上发布版本号
22+
version = '5.8'//maven 上发布版本号
2323
description = 'This is a very functional Toast'//描述,自己定义
2424
website = "https://github.com/getActivity/ToastUtils"//项目在github中的地址
2525
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ public void handleMessage(Message msg) {
7575
if (text != null) {
7676
mToast.setText(text);
7777
mToast.show();
78-
// 等这个 Toast 显示完后再继续显示,要加上一点延迟,不然在某些手机上 Toast 可能会来不及消失
79-
sendEmptyMessageDelayed(TYPE_CONTINUE, getToastDuration(text) + 100);
78+
// 等这个 Toast 显示完后再继续显示,要加上一些延迟,不然在某些手机上 Toast 可能会来不及消失
79+
sendEmptyMessageDelayed(TYPE_CONTINUE, getToastDuration(text) + 300);
8080
}else {
8181
isShow = false;
8282
}

0 commit comments

Comments
 (0)