美文网首页
Kotlin-对Intent携参取值的扩展-(借鉴)

Kotlin-对Intent携参取值的扩展-(借鉴)

作者: Cosecant | 来源:发表于2020-08-01 20:58 被阅读0次

    目的: 主要是使用Kotlin扩展属性处理Intent中数据的取值

    Kotlin的Property的读写扩展,下面简单举个栗子,下面案例中是为了获取Kotlin中的某个Boolean值,值可能存在于Intent中,也可能是Intent中的bundle中,具体类写法如下:

    class IntentExtraBoolean(
        private val key: String? = null,
        private val defaultValue: Boolean,
        private val isAssignIntoExtras: Boolean = true
    ) {
    
        private val KProperty<*>.extraName: String
            get() = this@IntentExtraBoolean.key ?: name
    
        operator fun getValue(intent: Intent, property: KProperty<*>): Boolean =
            intent.extras?.getBoolean(property.extraName, defaultValue)
                ?: intent.getBooleanExtra(property.extraName, defaultValue)
    
        operator fun setValue(intent: Intent, property: KProperty<*>, value: Boolean?) {
            when (isAssignIntoExtras) {
                true -> when (intent.extras) {
                    null -> intent.putExtras(bundleOf(property.extraName to (value ?: defaultValue)))
                    else -> intent.extras?.putBoolean(property.extraName, value ?: defaultValue)
                }
                else -> intent.putExtra(property.extraName, value ?: defaultValue)
            }
        }
    
    }
    

    调用方式:

    // 跳转地址页面中的传值
    fun Activity.startMyAddressListActivity(isNeedAddress: Boolean = true) =
            forward(MyAddressListActivity::class.java,
                    data = bundleOf("isNeedAddress" to isNeedAddress),
                    requestCode = if (isNeedAddress) ReqCodeConstants.ChooseAddr else 0)
    
    // 地址列表页面中的取值
    class MyAddressListActivity : BasicActivity<ActivityMyAddressListBinding>(), View.OnClickListener, OnRefreshListener {
    
        companion object {
            private val Intent.isNeedAddress by IntentExtraBoolean(defaultValue = false)
        }
    
       override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            val isNeedAddress = intent.isNeedAddress; //取得传递的值
       }
        .... // 省略多行代码
    }
    

    其他的取值方式亦如,可以照样画葫芦

    class IntentExtraBooleanArray(
        private val key: String? = null,
        private val isAssignIntoExtras: Boolean = true
    ) {
    
        private val KProperty<*>.extraName: String
            get() = this@IntentExtraBooleanArray.key ?: name
    
        operator fun getValue(intent: Intent, property: KProperty<*>): BooleanArray? =
            intent.extras?.getBooleanArray(property.extraName)
                ?: intent.getBooleanArrayExtra(property.extraName)
    
        operator fun setValue(intent: Intent, property: KProperty<*>, value: BooleanArray?) {
            when (isAssignIntoExtras) {
                true -> when (intent.extras) {
                    null -> intent.putExtras(bundleOf(property.extraName to value))
                    else -> intent.extras?.putBooleanArray(property.extraName, value)
                }
                else -> intent.putExtra(property.extraName, value)
            }
        }
    }
    

    这里做个记录,方便以后查阅!!!

    相关文章

      网友评论

          本文标题:Kotlin-对Intent携参取值的扩展-(借鉴)

          本文链接:https://www.haomeiwen.com/subject/krzlrktx.html