Skip to content

Commit 04aeb75

Browse files
committed
更新SpUtil。
1 parent a735de6 commit 04aeb75

6 files changed

Lines changed: 54 additions & 50 deletions

File tree

README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# AndroidUtilsKt
22

33

4+
45
1. 根据工具类文件名的英文字母顺序排序;
56
2. 接收类为“/”的表示该工具类并非扩展方式的写法,需要使用类名的形式调用里面的函数或属性;
67
3. 成员包含函数和属性,函数名称后面需要加“()”,属性则不用;
78
4. 常量另外写一个表格。
89

9-
1010
### Activity工具类:ActivityUtil
1111

1212
| 成员名称 | 作用 | 接收类 | 备注 |
@@ -36,15 +36,15 @@
3636
| getAppSize() | 获取应用大小,默认为本应用 | Context | 返回值单位为b |
3737
| getAppIcon() |获取应用图标,默认为本应用| Context | 失败时返回null |
3838

39-
------------
39+
------------
4040

4141
### 剪贴板工具类:ClipboardUtil
4242

4343
| 成员名称 | 作用 | 接收类 | 备注 |
4444
| ------------ | ------------ | ------------ | :------------: |
4545
| clipPlainText() | 复制纯文本 | Context | / |
4646

47-
------------
47+
------------
4848

4949
### 像素单位转换工具类:DensityUtil
5050
| 成员名称 | 作用 | 接收类 | 备注 |
@@ -54,7 +54,7 @@
5454
| sp2px() | sp转px | Context | / |
5555
| px2sp() | px转sp | Context | / |
5656

57-
------------
57+
------------
5858

5959
### 意图工具类:IntentUtil
6060

@@ -68,7 +68,7 @@
6868
| launchCamera() | 启动系统相机 | / | 动态权限 |
6969
| sendSMS() | 发送短信 | / | 动态权限 |
7070

71-
------------
71+
------------
7272

7373
### 软键盘工具类:KeyboardUtil
7474

@@ -79,7 +79,7 @@
7979
| toggleKeyboard() | 根据当前软键盘的状态做取反操作 | View | / |
8080

8181

82-
------------
82+
------------
8383

8484
### 网络状态工具类:NetworkUtil
8585
#### 常量
@@ -100,15 +100,15 @@
100100
| isMobileNet | 是否是移动网络 | Context | / |
101101
| isNetworkConnect | 网络是否连接 | Context | / |
102102

103-
------------
103+
------------
104104

105105
### 资源工具类:ResourceUtil
106106
| 成员名称 | 作用 | 接收类 | 备注 |
107107
| ------------ | ------------ | :------------: | :------------: |
108108
| getResColor() | 获取颜色 | Context | / |
109109
| getResDrawable() | 获取图片资源 | Context | 返回值可以为null |
110110

111-
------------
111+
------------
112112

113113
### 屏幕相关工具类:ScreenUtil
114114

@@ -125,7 +125,7 @@
125125
| isPortrait | 是否是竖屏 | Context | / |
126126
| setScreenBrightness() | 设置屏幕亮度 | Activity | 亮度范围为0~1,1为最亮,默认为-1 |
127127

128-
------------
128+
------------
129129

130130
### SharedPreferences工具类:SpUtil
131131

@@ -144,12 +144,12 @@
144144
| getSpFloat() | 取出存储的Float值 | Context | / |
145145
| putSpStrSet() | 存储StringSet | Context | / |
146146
| getSpStrSet() | 取出存储的StringSet | Context | / |
147-
| putSpNumber() | 保存Long、Float和Int数据 | Context | 不包含Double |
148-
| getSpNumber() | 取出保存Long、Float和Int数据 | Context | / |
147+
| putSp() | 保存数据 | Context | 数据类型由传入的值确定 |
148+
| getSp() |取出数据 | Context | 数据类型由传入的默认值确定 |
149149
| deleteSpKey() | 删除某条数据 | Context | / |
150150
| clearSp() | 清除SharedPreferences的数据 | 不输入表名则清除默认表中的数据 | / |
151151

152-
------------
152+
------------
153153

154154
### 状态栏工具类:StatusBarUtil
155155

@@ -163,7 +163,7 @@
163163
| setStatusBarDarkMode() | 设置状态栏白色字体图标(深色模式) | Activity | / |
164164
| setStatusBarLightMode() | 设置状态栏黑色字体图标(浅色模式) | Activity | / |
165165

166-
------------
166+
------------
167167

168168
### 时间工具类:TimeUtil
169169
| 成员名称 | 作用 | 接收类 | 备注 |
@@ -183,7 +183,7 @@
183183
| formatTimeYMDHMS | 将服务器时间格式转换为年月日时分秒 | String | / |
184184
| formatRelativeTime | 获取某个时间与当前时间的比较值 | String | / |
185185

186-
------------
186+
------------
187187

188188
### Toast工具类:ToastUtil
189189
已去除小米手机自带的应用名称。

androidutilskt/src/main/java/com/lindroid/androidutilskt/app/AndUtil.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ object AndUtil {
4545
/**
4646
* 设置全局的服务器时间格式
4747
*/
48-
4948
fun setServerTimeFormat(timeFormat: String) = this.apply { serverFormat = timeFormat }
5049
}
5150

androidutilskt/src/main/java/com/lindroid/androidutilskt/extension/SpUtil.kt

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,38 +59,40 @@ fun Context.putSpFloat(key: String, value: Float, fileName: String = defFileName
5959
fun Context.getSpFloat(key: String, defValue: Float = 0F, fileName: String = defFileName): Float =
6060
getSp(fileName).getFloat(key, defValue)
6161

62+
6263
/**
63-
* 保存long、float和int数据,double除外
64-
* putSpNumber方法的作用跟putSpInt、putSpLong和putSpFloat的作用是一样的
64+
* 保存数据,数据类型由传入的值确定
65+
* @throws IllegalArgumentException:数据类型不属于SharedPreferences能保存的类型
6566
*/
6667
@SuppressLint("ApplySharedPref")
67-
fun Context.putSpNumber(key: String, value: Number, fileName: String = defFileName): Boolean {
68-
val editor = getSp(fileName).edit()
69-
return when (value) {
70-
is Int -> editor.putInt(key, value).commit()
71-
is Float -> editor.putFloat(key, value).commit()
72-
is Long -> editor.putLong(key, value).commit()
73-
else -> {
74-
false
75-
}
68+
fun <T : Any> Context.putSp(key: String, value: T, fileName: String = defFileName): Boolean =
69+
with(getSp(fileName).edit()) {
70+
when (value) {
71+
is Int -> putInt(key, value)
72+
is Float -> putFloat(key, value)
73+
is Long -> putLong(key, value)
74+
is Double -> putFloat(key, value.toFloat())
75+
is Boolean -> putBoolean(key, value)
76+
is String -> putString(key, value)
77+
else -> throw IllegalArgumentException("This type can not be saved into SharedPreferences!")
78+
}.commit()
7679
}
77-
}
7880

7981
/**
80-
* 获取数值型数据
81-
* @param defValue 重要:根据默认值来判断数据类型,如果是double的话就转为float
82+
* 取出数据,数据类型由传入的默认值确定
83+
* @throws IllegalArgumentException:数据类型不属于SharedPreferences能保存的类型
8284
*/
83-
fun Context.getSpNumber(key: String, defValue: Number, fileName: String = defFileName): Number {
84-
val preference = getSp(fileName)
85-
return when (defValue) {
86-
is Int -> preference.getInt(key, defValue)
87-
is Float -> preference.getFloat(key, defValue)
88-
is Long -> preference.getLong(key, defValue)
89-
is Double -> preference.getFloat(key, defValue.toFloat())
90-
else -> {
91-
defValue
92-
}
93-
}
85+
@Suppress("UNCHECKED_CAST", "IMPLICIT_CAST_TO_ANY")
86+
fun <T : Any> Context.getSp(key: String, defValue: T, fileName: String = defFileName): T = with(getSp(fileName)) {
87+
when (defValue) {
88+
is Int -> getInt(key, defValue)
89+
is Float -> getFloat(key, defValue)
90+
is Long -> getLong(key, defValue)
91+
is String -> getString(key, defValue)
92+
is Boolean -> getBoolean(key, defValue)
93+
is Double -> getFloat(key, defValue.toFloat())
94+
else -> throw IllegalArgumentException("This type can not be found in SharedPreferences!")
95+
} as T
9496
}
9597

9698
/**
@@ -106,3 +108,5 @@ fun Context.deleteSpKey(key: String, fileName: String = defFileName): Boolean =
106108
fun Context.clearSp(fileName: String = defFileName): Boolean =
107109
getSp(fileName).edit().clear().commit()
108110

111+
112+

androidutilskt/src/main/java/com/lindroid/androidutilskt/extension/TimeUtil.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,26 @@ fun String.formatRelativeTime(pattern: String = serverFormat): String {
123123
val date = SimpleDateFormat(pattern, Locale.getDefault()).parse(this)
124124
val delta = currentTimeMillis - date.time
125125
return when {
126-
//xx分钟前
126+
//xx分钟前
127127
delta < 1L * ONE_HOUR -> {
128-
if (delta.toMinutes()<=1){
128+
if (delta.toMinutes() <= 1) {
129129
//不足一分钟显示1分钟前
130130
AndUtil.getString(R.string.time_one_minute_ago)
131-
}else{
131+
} else {
132132
String.format(AndUtil.getString(R.string.time_minute_ago), delta.toMinutes())
133133
// "${delta.toMinutes()}${AndUtil.getString(R.string.time_one_minute_ago)}"
134134
}
135135
}
136-
//xx小時前
136+
//xx小時前
137137
delta >= ONE_HOUR && delta < 24 * ONE_HOUR ->
138138
String.format(AndUtil.getString(R.string.time_hour_ago), delta.toHour())
139139
// "${delta.toHour()}${AndUtil.getString(R.string.time_one_hour_ago)}"
140-
//昨天
140+
//昨天
141141
delta >= 24 * ONE_HOUR && delta < 48 * ONE_HOUR -> AndUtil.getString(R.string.time_yesterday)
142-
//前天
142+
//前天
143143
delta >= 48 * ONE_HOUR && delta < 72 * ONE_HOUR -> AndUtil.getString(R.string.time_day_before_yesterday)
144-
//顯示年月日
144+
//顯示年月日
145145
delta >= 72 * ONE_HOUR -> this.formatTimeYMD()
146-
else ->this.formatTimeYMD()
146+
else -> this.formatTimeYMD()
147147
}
148148
}

app/src/main/java/com/lindroid/androidutilsktdemo/activity/CatalogActivity.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.lindroid.androidutilsktdemo.activity
22

33
import android.content.Intent
44
import android.widget.ArrayAdapter
5-
import com.lindroid.androidutilskt.extension.putSpString
65
import com.lindroid.androidutilsktdemo.R
76
import com.lindroid.androidutilsktdemo.activity.statusbar.StatusBarActivity
87
import com.lindroid.androidutilsktdemo.base.BaseActivity
@@ -49,6 +48,5 @@ class CatalogActivity(override val contentViewId: Int = R.layout.activity_catalo
4948
}.values.toList()
5049
startActivity(rightMap[0])
5150
}
52-
putSpString("hh", "aa")
5351
}
5452
}

app/src/main/java/com/lindroid/androidutilsktdemo/activity/ScreenActivity.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.widget.SeekBar
55
import com.lindroid.androidutilskt.extension.*
66
import com.lindroid.androidutilsktdemo.R
77
import com.lindroid.androidutilsktdemo.base.BaseActivity
8+
import com.youngfeng.snake.annotations.EnableDragToClose
89
import kotlinx.android.synthetic.main.activity_screen.*
910

1011

@@ -14,6 +15,8 @@ import kotlinx.android.synthetic.main.activity_screen.*
1415
* @function 屏幕相关工具类
1516
* @Description
1617
*/
18+
19+
@EnableDragToClose
1720
class ScreenActivity(override val contentViewId: Int = R.layout.activity_screen) : BaseActivity() {
1821

1922
override fun initView() {

0 commit comments

Comments
 (0)