Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b9215e9

Browse filesBrowse files
author
Quyunshuo
committed
Merge branch 'master' into demo
# Conflicts: # Lib_Common/src/main/java/com/quyunshuo/common/ui/BaseActivity.kt
2 parents 91b934b + bc9772a commit b9215e9
Copy full SHA for b9215e9

20 files changed

+790
-38
lines changed
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.quyunshuo.base.handler
2+
3+
import android.os.Handler
4+
import android.os.Looper
5+
import androidx.lifecycle.Lifecycle
6+
import androidx.lifecycle.LifecycleObserver
7+
import androidx.lifecycle.LifecycleOwner
8+
import androidx.lifecycle.OnLifecycleEvent
9+
10+
/**
11+
* @Author: QuYunShuo
12+
* @Time: 2020/9/17
13+
* @Class: LifecycleHandler
14+
* @Remark: 自动在UI销毁时移除msg和任务的Handler,不会有内存泄露
15+
*/
16+
class LifecycleHandler(
17+
private val lifecycleOwner: LifecycleOwner,
18+
looper: Looper = Looper.getMainLooper()
19+
) : Handler(looper), LifecycleObserver {
20+
21+
init {
22+
lifecycleOwner.lifecycle.addObserver(this)
23+
}
24+
25+
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
26+
fun onDestroy() {
27+
removeCallbacksAndMessages(null)
28+
lifecycleOwner.lifecycle.removeObserver(this)
29+
}
30+
}
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.quyunshuo.base.ktx
2+
3+
import android.text.InputFilter
4+
import android.widget.EditText
5+
6+
/**
7+
* @Author: QuYunShuo
8+
* @Time: 2020/9/17
9+
* @Class: EditTextKtx
10+
* @Remark: EditText相关扩展方法
11+
*/
12+
13+
/**
14+
* 过滤掉空格和回车
15+
*/
16+
fun EditText.filterBlankAndCarriageReturn() {
17+
this.filters =
18+
arrayOf(InputFilter { source, _, _, _, _, _ -> if (source == " " || source == "\n") "" else null })
19+
}
+75Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.quyunshuo.base.ktx
2+
3+
import android.content.Context
4+
import androidx.fragment.app.Fragment
5+
6+
/**
7+
* @Author: QuYunShuo
8+
* @Time: 2020/9/17
9+
* @Class: SizeUnitKtx
10+
* @Remark: 尺寸单位换算相关扩展属性
11+
*/
12+
13+
/**
14+
* dp 转 px
15+
*/
16+
fun Context.dp2px(dpValue: Float): Int {
17+
val scale = resources.displayMetrics.density
18+
return (dpValue * scale + 0.5f).toInt()
19+
}
20+
21+
/**
22+
* px 转 dp
23+
*/
24+
fun Context.px2dp(pxValue: Float): Int {
25+
val scale = resources.displayMetrics.density
26+
return (pxValue / scale + 0.5f).toInt()
27+
}
28+
29+
/**
30+
* sp 转 px
31+
*/
32+
fun Context.sp2px(spValue: Float): Int {
33+
val scale = resources.displayMetrics.scaledDensity
34+
return (spValue * scale + 0.5f).toInt()
35+
}
36+
37+
/**
38+
* px 转 sp
39+
*/
40+
fun Context.px2sp(pxValue: Float): Int {
41+
val scale = resources.displayMetrics.scaledDensity
42+
return (pxValue / scale + 0.5f).toInt()
43+
}
44+
45+
/**
46+
* dp 转 px
47+
*/
48+
fun Fragment.dp2px(dpValue: Float): Int {
49+
val scale = resources.displayMetrics.density
50+
return (dpValue * scale + 0.5f).toInt()
51+
}
52+
53+
/**
54+
* px 转 dp
55+
*/
56+
fun Fragment.px2dp(pxValue: Float): Int {
57+
val scale = resources.displayMetrics.density
58+
return (pxValue / scale + 0.5f).toInt()
59+
}
60+
61+
/**
62+
* sp 转 px
63+
*/
64+
fun Fragment.sp2px(spValue: Float): Int {
65+
val scale = resources.displayMetrics.scaledDensity
66+
return (spValue * scale + 0.5f).toInt()
67+
}
68+
69+
/**
70+
* px 转 sp
71+
*/
72+
fun Fragment.px2sp(pxValue: Float): Int {
73+
val scale = resources.displayMetrics.scaledDensity
74+
return (pxValue / scale + 0.5f).toInt()
75+
}

‎Lib_Base/src/main/java/com/quyunshuo/base/ktx/ContextKtx.kt renamed to ‎Lib_Base/src/main/java/com/quyunshuo/base/ktx/ToastKtx.kt

Copy file name to clipboardExpand all lines: Lib_Base/src/main/java/com/quyunshuo/base/ktx/ToastKtx.kt
+20-19Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import android.content.Context
44
import android.view.Gravity
55
import android.widget.Toast
66
import androidx.annotation.StringRes
7+
import androidx.fragment.app.Fragment
78

89
/**
910
* @Author: QuYunShuo
10-
* @Time: 2020/8/17
11-
* @Class: ContextKtx
12-
* @Remark: Context相关的扩展方法
11+
* @Time: 2020/9/17
12+
* @Class: ToastKtx
13+
* @Remark: Toast相关的扩展方法
1314
*/
1415

1516
/**
@@ -49,33 +50,33 @@ fun Context.centerToast(@StringRes resId: Int, duration: Int = Toast.LENGTH_SHOR
4950
}
5051

5152
/**
52-
* dp 转 px
53+
* Toast
54+
* @param text CharSequence 类型文本
5355
*/
54-
fun Context.dp2px(dpValue: Float): Int {
55-
val scale = resources.displayMetrics.density
56-
return (dpValue * scale + 0.5f).toInt()
56+
fun Fragment.toast(text: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
57+
context?.toast(text, duration)
5758
}
5859

5960
/**
60-
* px 转 dp
61+
* Toast
62+
* @param resId String 类型资源id
6163
*/
62-
fun Context.px2dp(pxValue: Float): Int {
63-
val scale = resources.displayMetrics.density
64-
return (pxValue / scale + 0.5f).toInt()
64+
fun Fragment.toast(@StringRes resId: Int, duration: Int = Toast.LENGTH_SHORT) {
65+
context?.toast(resId, duration)
6566
}
6667

6768
/**
68-
* sp 转 px
69+
* 居中Toast
70+
* @param text CharSequence 类型文本
6971
*/
70-
fun Context.sp2px(spValue: Float): Int {
71-
val scale = resources.displayMetrics.scaledDensity
72-
return (spValue * scale + 0.5f).toInt()
72+
fun Fragment.centerToast(text: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
73+
context?.centerToast(text, duration)
7374
}
7475

7576
/**
76-
* px 转 sp
77+
* 居中Toast
78+
* @param resId String 类型资源id
7779
*/
78-
fun Context.px2sp(pxValue: Float): Int {
79-
val scale = resources.displayMetrics.scaledDensity
80-
return (pxValue / scale + 0.5f).toInt()
80+
fun Fragment.centerToast(@StringRes resId: Int, duration: Int = Toast.LENGTH_SHORT) {
81+
context?.centerToast(resId, duration)
8182
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.