With the popularity of Kotlin, it is necessary to summarize the common tool classes used in development, including, of course, using Kotlin to extend Android native control methods and properties.
The main division of labor has two parts: class util and native control extension ext
-
The usage toast toast (” hello “)
-
Click the usage
button.click{ // todo }
-
Start the activity
Start + The activity to start (such as DemoActivity)
-
SharePreferenceUtil usage
1). var spValue by SharePreferenceUtil (“key”, “DefaultValue”)
2). set data spValue = “value”
3). get data spValue
-
NetworkUtil usage
1). isNetworkAvailable(context)
2). isConnected(context)
3). getNetworkType(context)
-
Gets the screen width and height and Dp/Px conversion usage
1). dp2px(context) or px2dp
2). screenWidth or screenHeight
-
RegularUtil usage
1). Id card is legal isIDCard(“no”)
2). Is the mobile phone number legal isMobile(“no”)
IsEmail (“xx.mail.com”)
4). Verify isUsername()
5). Format date check isDate()
-
EncodeUtil usage
1). Encode (input,””)
2). Decode (input,””)
3). Base64Encode (input: String), base64Decode(input: String?)
BinaryEncode (input: String), binaryDecode(input: String)
5). htmlEncode(input: CharSequence?) , htmlDecode (input: String?
. Continuous improvement
Just to illustrate the SharePreferenceUtil code,
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} /** * delete all data */ fun clearPreference() {pfs.edit ().clear().apply()} /** * Delete stored data */ fun clearPreference(key: String) {pfs.edit ().remove(key).apply()} /** * Serialize objects * @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} / * * * * @ deserialized object 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 ByteArrayInputStream objectInputStream. Close (). The close () return obj} / * * * query a key whether there have been a * * * @ @ param key return * / fun contains(key: String): Boolean {return prefs.contains(key)} /** * returns all key/value pairs ** @param context * @return */ fun getAll(): Map<String, *> { return prefs.all }Copy the code
}
…
Finally, attach the Github address github.com/AlbertShen0…
Welcome to use, if you feel good, don’t forget to star oh