Skip to content

Commit

Permalink
新增自动区分资源id和int整数,优化XToast
Browse files Browse the repository at this point in the history
  • Loading branch information
880634 committed Sep 18, 2018
1 parent a0ed7de commit f871400
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#### 集成步骤

dependencies {
implementation 'com.hjq:toast:2.0'
implementation 'com.hjq:toast:2.2'
}

#### 初始化Toast
Expand Down Expand Up @@ -39,7 +39,7 @@

#### 框架亮点

* 功能强大:不分主次线程都可以弹出Toast,防崩溃处理
* 功能强大:不分主次线程都可以弹出Toast,自动区分资源id和int类型

* 使用简单:只需传入文本,会自动根据文本长度决定吐司显示的时长

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.hjq.toast.demo"
minSdkVersion 14
targetSdkVersion 26
versionCode 20
versionName "2.0"
versionCode 22
versionName "2.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ android {

defaultConfig {
targetSdkVersion 26
versionCode 20
versionName "2.0"
versionCode 22
versionName "2.2"
}
}

publish {
userOrg = 'getactivity'//填写bintray用户名,注意大小写
groupId = 'com.hjq'//定义的maven group id最终引用形式
artifactId = 'toast'//maven的artifact id
version = '2.0'//maven 上发布版本号
version = '2.2'//maven 上发布版本号
description = 'This is a very functional Toast'//描述,自己定义
website = "https://github.com/getActivity/ToastUtils"//项目在github中的地址
}
Expand Down
30 changes: 26 additions & 4 deletions library/src/main/java/com/hjq/toast/ToastUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.hjq.toast;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.os.Looper;
Expand Down Expand Up @@ -70,10 +71,28 @@ public static void init(Context context) {
* @param object 对象
*/
public static void show(Object object) {
if (object != null) {
show(object.toString());
}else {
show("null");
show(object != null ? object.toString() : "null");
}

/**
* 显示一个吐司
*
* @param id 如果传入的是正确的string id就显示对应字符串
* 如果不是则显示一个整数的string
*/
public static void show(int id) {

//吐司工具类还没有被初始化,必须要先调用init方法进行初始化
if (sToast == null) {
throw new IllegalStateException("ToastUtils has not been initialized");
}

try {
//如果这是一个资源id
show(sToast.getView().getContext().getResources().getText(id));
} catch (Resources.NotFoundException ignored) {
//如果这是一个int类型
show(String.valueOf(id));
}
}

Expand Down Expand Up @@ -124,6 +143,9 @@ public static Toast getToast() {
* 给当前Toast设置新的布局
*/
public static void setView(View view) {
if (view == null) {
throw new IllegalArgumentException("Views cannot be empty");
}
sToast.setView(view);
}

Expand Down
11 changes: 3 additions & 8 deletions library/src/main/java/com/hjq/toast/XToast.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.hjq.toast;

import android.content.Context;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
Expand All @@ -18,11 +17,7 @@ final class XToast extends Toast {
private TextView mTextView;

XToast(Context context) {
this(context, Looper.getMainLooper());
}

XToast(Context context, Looper looper) {
super(context, looper);
super(context);
}

@Override
Expand All @@ -39,7 +34,7 @@ public final void setView(View view) {
}
}
}
//如果设置的布局没有包含一个TextView则抛出异常,必须要有一个TextView
//如果设置的布局没有包含一个TextView则抛出异常,必须要包含一个TextView作为Message对象
throw new IllegalArgumentException("The layout must contain a TextView");
}

Expand All @@ -51,4 +46,4 @@ public final void setText(CharSequence s) {
super.setText(s);
}
}
}
}

0 comments on commit f871400

Please sign in to comment.