Skip to content

Commit

Permalink
优化调整
Browse files Browse the repository at this point in the history
  • Loading branch information
YeChao committed May 25, 2021
1 parent 61eede4 commit d1e95dd
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 52 deletions.
18 changes: 1 addition & 17 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
![](https://img.shields.io/badge/language-Java%20&%20kotlin-orange.svg)
![](https://img.shields.io/hexpm/l/plug.svg)

最近更新 2021.01.26
最近更新 2021.05.25

### How to use

Expand All @@ -22,13 +22,13 @@ allprojects {
```
dependencies {
//kotlin 版本
implementation 'com.github.yechaoa.YUtils:yutilskt:3.2.0'
implementation 'com.github.yechaoa.YUtils:yutilskt:3.2.1'
//java 版本
implementation 'com.github.yechaoa.YUtils:yutils:3.2.0'
implementation 'com.github.yechaoa.YUtils:yutils:3.2.1'
//kotlin && java 版本
implementation 'com.github.yechaoa:YUtils:3.2.0'
implementation 'com.github.yechaoa:YUtils:3.2.1'
}
```

Expand All @@ -50,7 +50,9 @@ LogUtil.setIsLog(true);
* void init(Application app)<br>Application 中初始化

* Application getApp()<br>获取全局上下文
* Application getApp()<br>获取Application

* Context getAppContext()<br>获取全局上下文

* void showLoading(Activity activity,String msg)<br>Loading加载框

Expand Down Expand Up @@ -85,7 +87,11 @@ LogUtil.setIsLog(true);
* void showCenter(final String msg)<br>showCenter 居中显示

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


> kotlin分支中添加了一个Toast扩展,比之前使用更加简单方便,调用示例:
> "111".show() -> 111
> 111.show() -> 111
> 111.show("222") -> 222
> ## 3.LogUtil(日志打印,带方法行数链接,可超长打印)
Expand Down
24 changes: 18 additions & 6 deletions app/src/main/java/com/yechaoa/app/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package com.yechaoa.app

import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.os.Looper
import android.widget.TextView
import com.google.android.material.snackbar.Snackbar
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.yechaoa.yutilskt.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.concurrent.thread


class MainActivity : AppCompatActivity() {

@RequiresApi(Build.VERSION_CODES.M)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand All @@ -31,7 +35,6 @@ class MainActivity : AppCompatActivity() {

button.setOnClickListener {
SpUtil.setStringSet("testStringSet", set)
//SpUtil.getBoolean("111",true)
}

button2.setOnClickListener {
Expand All @@ -51,9 +54,6 @@ class MainActivity : AppCompatActivity() {
LogUtil.i("" + ActivityUtil.currentActivityName)
}

// ActivityUtil.start(MainActivity::class.java)
// ActivityUtil.finish(this)

btn_display.setOnClickListener {
LogUtil.i("" + DisplayUtil.getStatusBarHeight() + "---" + DisplayUtil.getScreenHeight())
LogUtil.i("" + DisplayUtil.getActionBarHeight() + "---" + DisplayUtil.getNavBarHeight())
Expand All @@ -62,5 +62,17 @@ class MainActivity : AppCompatActivity() {
btn_sim.setOnClickListener {
ToastUtil.show(if (YUtils.hasSim()) "有sim卡" else "无sim卡")
}

btn_thread.setOnClickListener {
LogUtil.d("" + mainLooper.isCurrentThread)
LogUtil.d("" + Looper.myLooper()!!.isCurrentThread)
val b = mainLooper == Looper.myLooper()
LogUtil.d("" + b)
thread {
ToastUtil.show("213")
}

}

}
}
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,10 @@
android:layout_height="wrap_content"
android:text="是否有sim卡" />

<Button
android:id="@+id/btn_thread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打印线程" />

</LinearLayout>
9 changes: 5 additions & 4 deletions yutils/src/main/java/com/yechaoa/yutils/ToastUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.yechaoa.yutils;

import android.annotation.SuppressLint;
import android.os.Looper;
import android.view.Gravity;
import android.widget.Toast;

Expand All @@ -23,7 +24,7 @@ public class ToastUtil {
*/
@Deprecated
public static void showToast(final String msg) {
if ("main".equals(Thread.currentThread().getName())) {
if (Looper.getMainLooper() == Looper.myLooper()) {
createToast(msg);
} else {
ActivityUtil.getCurrentActivity().runOnUiThread(new Runnable() {
Expand All @@ -41,7 +42,7 @@ public void run() {
* @param msg 需要显示的参数
*/
public static void show(final String msg) {
if ("main".equals(Thread.currentThread().getName())) {
if (Looper.getMainLooper() == Looper.myLooper()) {
createToast(msg);
} else {
ActivityUtil.getCurrentActivity().runOnUiThread(new Runnable() {
Expand Down Expand Up @@ -75,7 +76,7 @@ private static void createToast(String msg) {
*/
@Deprecated
public static void showCenterToast(final String msg) {
if ("main".equals(Thread.currentThread().getName())) {
if (Looper.getMainLooper() == Looper.myLooper()) {
createCenterToast(msg);
} else {
ActivityUtil.getCurrentActivity().runOnUiThread(new Runnable() {
Expand All @@ -93,7 +94,7 @@ public void run() {
* @param msg 需要显示的参数
*/
public static void showCenter(final String msg) {
if ("main".equals(Thread.currentThread().getName())) {
if (Looper.getMainLooper() == Looper.myLooper()) {
createCenterToast(msg);
} else {
ActivityUtil.getCurrentActivity().runOnUiThread(new Runnable() {
Expand Down
6 changes: 5 additions & 1 deletion yutils/src/main/java/com/yechaoa/yutils/YUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,16 @@ public static Application getApplication() {

public static Application getApp() {
if (null == mApp) {
throw new NullPointerException("YUtils未在Application中初始化");
throw new NullPointerException("YUtils is not initialized in application");
} else {
return mApp;
}
}

public static Context getAppContext() {
return getApp().getApplicationContext();
}

/**
* 获取屏幕宽度 改为DisplayUtil.getScreenWidth()
*/
Expand Down
14 changes: 14 additions & 0 deletions yutilskt/src/main/java/com/yechaoa/yutilskt/ToastExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.yechaoa.yutilskt

import android.widget.Toast

/**
* Created by yechao on 2021/5/25.
* Describe : Toast 扩展
* "111".show() -> 111
* 111.show() -> 111
* 111.show("222") -> 222
*/
fun Any.show(msg: String = this.toString(), duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(YUtils.getAppContext(), msg, duration).show()
}
15 changes: 8 additions & 7 deletions yutilskt/src/main/java/com/yechaoa/yutilskt/ToastUtil.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.yechaoa.yutilskt

import android.annotation.SuppressLint
import android.os.Looper
import android.view.Gravity
import android.widget.Toast

Expand All @@ -19,17 +20,17 @@ object ToastUtil {
*
* @param msg 需要显示的参数
*/
@Deprecated("简化调用,使用show(msg)即可", ReplaceWith("ToastUtilKt.show(msg)"))
@Deprecated("简化调用,使用show(msg)即可", ReplaceWith("ToastUtil.show(msg)"))
fun showToast(msg: String) {
if ("main" == Thread.currentThread().name) {
if (Looper.getMainLooper() == Looper.myLooper()) {
createToast(msg)
} else {
ActivityUtil.currentActivity?.runOnUiThread { createToast(msg) }
}
}

fun show(msg: String) {
if ("main" == Thread.currentThread().name) {
if (Looper.getMainLooper() == Looper.myLooper()) {
createToast(msg)
} else {
ActivityUtil.currentActivity?.runOnUiThread { createToast(msg) }
Expand All @@ -56,17 +57,17 @@ object ToastUtil {
*
* @param msg 需要显示的参数
*/
@Deprecated("简化调用,使用showCenter(msg)即可", ReplaceWith("ToastUtilKt.showCenter(msg)"))
@Deprecated("简化调用,使用showCenter(msg)即可", ReplaceWith("ToastUtil.showCenter(msg)"))
fun showCenterToast(msg: String) {
if ("main" == Thread.currentThread().name) {
if (Looper.getMainLooper() == Looper.myLooper()) {
createCenterToast(msg)
} else {
ActivityUtil.currentActivity!!.runOnUiThread { createCenterToast(msg) }
}
}

fun showCenter(msg: String) {
if ("main" == Thread.currentThread().name) {
if (Looper.getMainLooper() == Looper.myLooper()) {
createCenterToast(msg)
} else {
ActivityUtil.currentActivity!!.runOnUiThread { createCenterToast(msg) }
Expand Down Expand Up @@ -94,7 +95,7 @@ object ToastUtil {
* onDestroy时调用,或者onPause
* 当前页面finish之后在下一个页面不会显示
*/
@Deprecated("简化调用,使用cancel()即可", ReplaceWith("ToastUtilKt.cancel()"))
@Deprecated("简化调用,使用cancel()即可", ReplaceWith("ToastUtil.cancel()"))
fun cancelToast() {
toast?.cancel()
}
Expand Down
9 changes: 7 additions & 2 deletions yutilskt/src/main/java/com/yechaoa/yutilskt/YUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ object YUtils {
if (this::mApp.isInitialized) {
return mApp
} else {
throw UninitializedPropertyAccessException("YUtils未在Application中初始化")
throw UninitializedPropertyAccessException("YUtils is not initialized in application")
}
}

fun getAppContext(): Context {
return getApp().applicationContext
}

/**
* 获取屏幕宽度
*/
Expand Down Expand Up @@ -235,7 +239,8 @@ object YUtils {
* 关闭软键盘
*/
fun closeSoftKeyboard() {
val inputManger = ActivityUtil.currentActivity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
val inputManger =
ActivityUtil.currentActivity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManger.hideSoftInputFromWindow(ActivityUtil.currentActivity!!.window.decorView.windowToken, 0)
}

Expand Down

0 comments on commit d1e95dd

Please sign in to comment.