随着Kotlin的普及,很有必要对开发中常用的工具类总结整理,当然包括
利用kotlin扩展Android原生控件方法、属性。
主要分工具类util跟原生控件扩展ext两部分
-
toast 用法
toast("hello")
-
click 用法
button.click{ // todo }
- 启动activity
start+要启动的activity (比如 DemoActivity)
-
SharePreferenceUtil 用法
1). var spValue by SharePreferenceUtil ("key", "DefaultValue") 2). set data spValue = "value" 3). get data spValue
-
NetworkUtil 用法
1). isNetworkAvailable(context) 2). isConnected(context) 3). getNetworkType(context)
-
获取屏幕宽高以及Dp/Px转化用法
1). dp2px(context) or px2dp 2). screenWidth or screenHeight
-
RegularUtil 用法
1). 身份证是否合法 isIDCard("no") 2). 手机号是否合法 isMobile("no") 3). isEmail是否合法 isEmail("xx.mail.com") 4). 用户名校验 isUsername() 5). 格式日期校验 isDate()
-
EncodeUtil 用法
1). 普通字符串编码 encode(input,"") 2). 普通字符串解码 decode(input,"") 3). base64Encode(input: String),base64Decode(input: String?) 4). binaryEncode(input: String),binaryDecode(input: String) 5). htmlEncode(input: CharSequence?),htmlDecode(input: String?)
在此,仅列举一下 SharePreferenceUtil代码,
class SharePreferenceUtil<T>(val name: String, private val default: T) : ReadWriteProperty<Any?, T> {
private val prefs: SharedPreferences by lazy {
appCtx.getSharedPreferences("default",Context.MODE_PRIVATE)
}
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
return getValue(name, default)
}
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
putValue(name, value)
}
@SuppressLint("CommitPrefEdits")
private fun <T> putValue(name: String, value: T) = with(prefs.edit()) {
when (value) {
is Long -> putLong(name, value)
is String -> putString(name, value)
is Int -> putInt(name, value)
is Boolean -> putBoolean(name, value)
is Float -> putFloat(name, value)
else -> putString(name, serialize(value))
}.apply()
}
@Suppress("UNCHECKED_CAST")
fun <T> getValue(name: String, default: T): T = with(prefs) {
val res: Any = when (default) {
is Long -> getLong(name, default)
is String -> this!!.getString(name, default)!!
is Int -> getInt(name, default)
is Boolean -> getBoolean(name, default)
is Float -> getFloat(name, default)
else -> deSerialization(getString(name, serialize(default)))
}
return res as T
}
/**
* 删除全部数据
*/
fun clearPreference() {
prefs.edit().clear().apply()
}
/**
* 根据key删除存储数据
*/
fun clearPreference(key: String) {
prefs.edit().remove(key).apply()
}
/**
* 序列化对象
* @param person
* *
* @return
* *
* @throws IOException
*/
@Throws(IOException::class)
private fun <A> serialize(obj: A): String {
val byteArrayOutputStream = ByteArrayOutputStream()
val objectOutputStream = ObjectOutputStream(
byteArrayOutputStream
)
objectOutputStream.writeObject(obj)
var serStr = byteArrayOutputStream.toString("ISO-8859-1")
serStr = java.net.URLEncoder.encode(serStr, "UTF-8")
objectOutputStream.close()
byteArrayOutputStream.close()
return serStr
}
/**
* 反序列化对象
* @param str
* *
* @return
* *
* @throws IOException
* *
* @throws ClassNotFoundException
*/
@Suppress("UNCHECKED_CAST")
@Throws(IOException::class, ClassNotFoundException::class)
private fun <A> deSerialization(str: String?): A {
val redStr = java.net.URLDecoder.decode(str, "UTF-8")
val byteArrayInputStream = ByteArrayInputStream(
redStr.toByteArray(charset("ISO-8859-1"))
)
val objectInputStream = ObjectInputStream(
byteArrayInputStream
)
val obj = objectInputStream.readObject() as A
objectInputStream.close()
byteArrayInputStream.close()
return obj
}
/**
* 查询某个key是否已经存在
*
* @param key
* @return
*/
fun contains(key: String): Boolean {
return prefs.contains(key)
}
/**
* 返回所有的键值对
*
* @param context
* @return
*/
fun getAll(): Map<String, *> {
return prefs.all
}
}
···
最后,附上Github地址,https://github.com/AlbertShen0211/Android-Control-Extension
欢迎使用,觉得好的话,不要忘了给star哦!