Android Kotlin测试中的Intent putExtra值
创始人
2024-08-14 14:00:13
0次

在Android Kotlin测试中,可以使用以下方法为Intent的putExtra方法设置值:

  1. 使用Intent的putExtra方法设置基本数据类型的值,如字符串、整数、布尔值等。
val intent = Intent()
intent.putExtra("key", "value")
intent.putExtra("count", 10)
intent.putExtra("isSuccess", true)
  1. 使用Intent的putExtra方法设置Parcelable对象的值。首先,需要在被传递的对象类中实现Parcelable接口。
data class User(val name: String, val age: Int) : Parcelable {
    constructor(parcel: Parcel) : this(
        parcel.readString() ?: "",
        parcel.readInt()
    )
    
    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(name)
        parcel.writeInt(age)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator {
        override fun createFromParcel(parcel: Parcel): User {
            return User(parcel)
        }

        override fun newArray(size: Int): Array {
            return arrayOfNulls(size)
        }
    }
}

val user = User("John", 25)
intent.putExtra("user", user)
  1. 使用Intent的putExtra方法设置序列化对象的值。被传递的对象类需要实现Serializable接口。
data class Product(val name: String, val price: Double) : Serializable

val product = Product("Phone", 500.0)
intent.putExtra("product", product)

注意:在测试中,可以使用Intent的getStringExtra、getIntExtra、getBooleanExtra等方法获取putExtra设置的值,并进行断言或验证。

val value = intent.getStringExtra("key")
assertEquals("value", value)

val count = intent.getIntExtra("count", 0)
assertEquals(10, count)

val isSuccess = intent.getBooleanExtra("isSuccess", false)
assertTrue(isSuccess)

val user = intent.getParcelableExtra("user")
assertEquals("John", user?.name)
assertEquals(25, user?.age)

val product = intent.getSerializableExtra("product") as? Product
assertEquals("Phone", product?.name)
assertEquals(500.0, product?.price)

相关内容

热门资讯

2027版辅助器!上品休闲辅助... 2027版辅助器!上品休闲辅助器多功能AI脚本,上品休闲都是是有辅助器脚本1、上品休闲免费脚本咨询教...
2027版辅助器!丫丫老陕麻将... 2027版辅助器!丫丫老陕麻将辅助器多功能AI插件,丫丫老陕麻将真是真的有辅助器插件该软件可以轻松地...
2027版开挂!丽水茶苑松阳麻... 2027版开挂!丽水茶苑松阳麻将开挂多功能AI脚本,丽水茶苑松阳麻将真是是真的有开挂脚本小薇(辅助器...
2027版辅助器!川麻换三张辅... 2027版辅助器!川麻换三张辅助器多功能AI攻略,川麻换三张一直是有辅助器攻略1、川麻换三张免费辅助...
2027版外挂!桃源麻将外挂多... 2027版外挂!桃源麻将外挂多功能AI插件,桃源麻将本来有外挂插件1、在桃源麻将插件功能辅助器技巧中...
2027版开挂!圈友建瓯麻将开... 2027版开挂!圈友建瓯麻将开挂多功能AI技巧,圈友建瓯麻将确实真的是有开挂技巧1、游戏颠覆性的策略...
2027版辅助!欢乐互娱辅助多... 2027版辅助!欢乐互娱辅助多功能AI技巧,欢乐互娱一直真的有辅助技巧1、欢乐互娱免费辅助多个强度级...
2027版辅助!闲来陕西麻将辅... 2027版辅助!闲来陕西麻将辅助多功能AI技巧,闲来陕西麻将一直有辅助技巧1、闲来陕西麻将脚本辅助下...
2027版辅助器!华淳麻将辅助... 2027版辅助器!华淳麻将辅助器多功能AI软件,华淳麻将好像真的是有辅助器软件1、不需要AI权限,帮...
2027版辅助器!一乐棋牌辅助... 2027版辅助器!一乐棋牌辅助器多功能AI攻略,一乐棋牌都是是真的有辅助器攻略小薇(辅助器软件下载)...