分类 安卓 下的文章

常用功能,其它上网就可以:

//状态栏
implementation 'com.gyf.immersionbar:immersionbar:3.0.0'

在每个ActivityOnCreate方法增加这个就可以

ImmersionBar.with(this)
    .transparentBar() // 透明
    .hideBar(BarHide.FLAG_HIDE_BAR) // 隐藏状态和导航条
    .init()

先引入库:

implementation 'com.tencent:mmkv-static:1.2.10'

这封装一个类

import android.widget.EditText
import android.widget.TextView
import androidx.annotation.Nullable
import com.tencent.mmkv.MMKV
import kotlinx.android.synthetic.main.activity_login_by_password.*
import java.lang.Exception

/**
 * 保存本地数据KEY
 */
object Save {
    /**
     * 常用保存
     */
    fun set(key: String, @Nullable value: Any) {
        when (value) {
            is String -> MMKV.defaultMMKV().encode(key, value)
            is Boolean -> MMKV.defaultMMKV().encode(key, value)
            is Long -> MMKV.defaultMMKV().encode(key, value)
            is Int -> MMKV.defaultMMKV().encode(key, value)
            is Double -> MMKV.defaultMMKV().encode(key, value)
            is Float -> MMKV.defaultMMKV().encode(key, value)
            else -> throw Exception("暂时不支持这类型");
        }
    }

    /**
     * 读取
     */
    fun get(key: String): String? {
        return MMKV.defaultMMKV().decodeString(key)
    }
    fun getLong(key: String): Long {
        return MMKV.defaultMMKV().decodeLong(key)
    }
    fun getInt(key: String): Int {
        return MMKV.defaultMMKV().decodeInt(key)
    }
    fun getBool(key: String): Boolean {
        return MMKV.defaultMMKV().decodeBool(key)
    }
    fun getDouble(key: String): Double {
        return MMKV.defaultMMKV().decodeDouble(key)
    }
    fun getFloat(key: String): Float {
        return MMKV.defaultMMKV().decodeFloat(key)
    }

    /**
     * 直接将值保存到EditText
     */
    fun setEditText(key: String, edit: EditText) {
        var value = MMKV.defaultMMKV().decodeString(key)
        value.let {
            edit.setText(it)
        }
    }

    /**
     * 直接将值保存到TextView
     */
    fun setTextView(key: String, edit: TextView) {
        var value = MMKV.defaultMMKV().decodeString(key)
        value.let {
            edit.text = it
        }
    }
}

类型判断并且转成类型:

fun hasPrefix(x: Any) = when(x) {
    is String -> x.startsWith("prefix")
    else -> false
}

范围:
when (x) {

    in 1..10 -> print("x is in the range")
    in validNumbers -> print("x is valid")
    !in 10..20 -> print("x is outside the range")
    else -> print("none of the above")
}

示例代码声明

class Developer (@JvmField val name: String, val ide: String)

使用@JvmField,我们在Java中调用的时候,可以直接使用属性名,而不是对应的getter方法。

//test jvmField

Developer developer = new Developer("Andy", "Android Studio");
System.out.println(developer.getIde());// not using JvmField
System.out.println(developer.name);// using JvmField