Skip to content

yechaoa/YUtils

Repository files navigation

Android快速开发工具集合——YUtils

How to use

  • Step 1 project:build.gradle
	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}
  • Step 2 app:build.gradle(以下3种引用方式,三选一即可)
dependencies {
    //kotlin 版本
    implementation 'com.github.yechaoa.YUtils:yutilskt:3.4.0'

    //java 版本
    implementation 'com.github.yechaoa.YUtils:yutils:3.4.0'

    //kotlin && java 版本
    implementation 'com.github.yechaoa:YUtils:3.4.0'
}

一、初始化(Initialization)

in your Application

//初始化
YUtils.init(this);

//打印开关(可选,默认false不打印)
LogUtil.setIsLog(true);

二、功能(Feature)

1.YUtils(各种小工具)

  • void init(Application app)
    Application 中初始化

  • Application getApp()
    获取Application

  • Context getAppContext()
    获取全局上下文

  • void showLoading(Activity activity,String msg)
    Loading加载框,kotlin版本参数更多

  • void hideLoading()
    hideLoading

  • boolean getLoadingDialog()
    获取dialog

  • boolean loadingIsShowing()
    loading是否显示

  • void finishBySleep(final long millis)
    根据时间休眠然后关闭当前页面,比如:3秒自动返回

  • String getVersionName()
    获取版本名

  • int getVersionCode()
    获取版本号

  • boolean checkPhoneNumber(String number)
    校验手机号

  • String MD5(String data)
    MD5加密

  • 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 showSoftKeyboard(View view)
    弹出软键盘

  • void closeSoftKeyboard()
    关闭软键盘

  • Boolean hasSim()
    是否有sim卡 即设备是否可以拨打电话等

2.ToastUtil(任意线程,不重复显示,可取消).

  • show(final String msg)
    底部显示(默认),任何线程且不会超长显示

  • void showCenter(final String msg)
    showCenter 居中显示

  • void cancel()
    取消Toast,onDestroy时调用,或onPause,当前页面finish之后在下一个页面不会再显示

kotlin分支中添加了一个Toast扩展,比之前使用更加简单方便,调用示例:
"111".show() -> 111
111.show() -> 111
111.show("222") -> 222

3.LogUtil(日志打印,带方法行数链接,可超长打印)

  • 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)

4.ActivityUtil(Activity管理)

  • Activity getCurrentActivity()
    获得当前栈顶Activity

  • String getCurrentActivityName()
    获得当前Activity名字

  • void start(Class<?> targetActivity)
    启动指定Activity 无参

  • void start(Class<?> targetActivity, Bundle bundle)
    启动指定Activity,带Bundle参数(kotlin调用参数可选)

  • void finish(Activity activity)
    关闭指定Activity

  • void closeAllActivity()
    关闭所有Activity

5.SpUtil(SharedPreferences)

  • 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()
    移除所有

6.ShareUtil(调用系统分享)

  • void shareImage(String title, Uri uri)
    分享图片

  • void shareText(String title, String text)
    分享文字

7.TimeUtil

  • String getDate()
    获取当前年月日

  • String getTime()
    获取当前时分秒

  • String getDateAndTime()
    获取当前年月日时分秒

  • Long getTimeForLong()
    获取当前时间,返回Long类型

  • String formatDate(String mDate)
    转换为年月日

8.ParseUtil(直接解析 Json)

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中的一级字段

9.GsonUtil

  • String GsonString(Object object)
    转成json

  • T GsonToBean(String gsonString, Class cls)
    转成bean

  • List GsonToList(String gsonString, Class cls)
    转成list

  • GsonToMaps(String gsonString)

10.DisplayUtil

  • int dp2px(float dp)
    dp2px

  • float px2dp(int px)
    px2dp

  • int getScreenWidth()
    获取屏幕宽度

  • int getScreenHeight()
    获取屏幕高度

  • int getStatusBarHeight()
    获取状态栏高度

  • int getActionBarHeight()
    获取ActionBar高度

  • int getNavBarHeight()
    获取导航栏高度


近期更新(Change Log)

3.4.0(2024-04-06)

  • Upgrade gradle version to 7.5

3.3.3(2024-04-01)

  • Upgrade targetSdkVersion 30 to 31
  • Upgrade gradle to 6.7.1

3.2.2(2021-10-16)

  • optimized code

3.2.1(2021-05-25)

  • 优化调整
  • 添加Toast扩展

3.2.0(2021-01-26)

  • targetSdkVersion 30
  • ToastUtil适配Android 11

混淆

#YUtils
-keep class com.yechaoa.yutils.**{*;}

关于(About)

License

   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.