- 3.0.0新增了
kotlin
版本,除了调用方式遵循kotlin的语法之外,类名的区别在于添加了后缀Kt
,比如YUtilsKt
,其他以此类推; - 此外,java版本和kotlin版本都删除了
ExitUtils
- kotlin版本没有
GsonUtil
- Step 1 project:build.gradle
allprojects {
repositories {
...
maven { url 'https://www.jitpack.io' }
}
}
- Step 2 app:build.gradle
dependencies {
implementation 'com.github.yechaoa:YUtils:3.0.2'
}
//初始化
YUtils.initialize(this);
//设置打印开关
LogUtil.setIsLog(true);
//注册Activity生命周期
registerActivityLifecycleCallbacks(ActivityUtil.getActivityLifecycleCallbacks());
-
void initialize(Application app)
Application 中初始化 -
Application getApplication()
获取全局上下文 -
int getScreenWidth()
获取屏幕宽度 -
int getScreenHeight()
获取屏幕高度 -
void showLoading(Activity activity,String msg)
Loading加载框 -
void hideLoading()
hideLoading -
void finishBySleep(final long millis)
根据时间休眠然后关闭当前页面,比如:3秒自动返回 -
String getVersionName()
获取版本名 -
int getVersionCode()
获取版本号 -
boolean checkPhoneNumber(String mobiles)
校验手机号 -
String MD5(String data)
MD5加密 -
int dp2px(float dp)
dp2px -
float px2dp(int px)
px2dp -
void copyToClipboard(String text)
复制文本到粘贴板 -
View Foreground(View view, int color, int start, int end)
字体高亮(TextView、EditText、Button)like this ---》 YUtils.Foreground(mEditText, Color.RED,1,2); -
void closeSoftKeyboard()
关闭软键盘
-
showToast(final String msg)
底部显示(默认),任何线程且不会超长显示 -
void showCenterToast(final String msg)
showCenterToast 居中显示 -
void cancelToast()
取消Toast,onDestroy时调用,或onPause,当前页面finish之后在下一个页面不会再显示
-
void setIsLog(boolean isLog)
是否打印 -
setIsLog(boolean isLog, String tag)
带全局tag -
void i(String msg)
不带tag -
void i(String TAG, String msg)
带tag -
void d(String msg)
-
void d(String TAG, String msg)
-
void e(String msg)
-
void e(String TAG, String msg)
-
Activity getCurrentActivity()
获得当前栈顶Activity -
String getCurrentActivityName()
获得当前Activity名字 -
void finishActivity(Activity activity)
关闭当前Activity -
void closeAllActivity()
关闭所有Activity
-
void setString(String key, String value)
String -
String getString(String key)
-
void setStringSet(String key, Set value)
StringSet -
Set getStringSet(String key)
-
void setInt(String key, String value)
Int -
String getInt(String key)
-
void setBoolean(String key, String value)
Boolean -
String getBoolean(String key)
-
void setFloat(String key, String value)
Float -
String getFloat(String key)
-
void setLong(String key, String value)
Long -
String getLong(String key)
-
void removeByKey(String key)
根据key移除 -
void removeAll()
移除所有
-
void shareImage(String title, Uri uri)
分享图片 -
void shareText(String title, String text)
分享文字
-
String getDate()
获取当前年月日 -
String getTime()
获取当前时分秒 -
String getDateAndTime()
获取当前年月日时分秒 -
Long getTimeForLong()
获取当前时间,返回Long类型 -
String formatDate(String mDate)
转换为年月日
2.0.8版本之后简化了解析方式,且不兼容更新
like this
{
"code": "0",
"data": "修改成功",
"flag": true,
"info": null
}
-
String parseCode(String response)
解析Code -
boolean parseFlag(String response)
解析Flag -
String parseData(String response)
解析data -
String parseInfo(String response)
解析info -
String parseByKey(String response, String key)
根据key值解析,只支持json中的一级字段
-
String GsonString(Object object)
转成json -
T GsonToBean(String gsonString, Class cls)
转成bean -
List GsonToList(String gsonString, Class cls)
转成list -
GsonToMaps(String gsonString)
混淆
#YUtils
-keep class com.yechaoa.yutils.**{*;}
Copyright 2017 yechaoa
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.