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)

相关内容

热门资讯

反观!wepoker科技辅助器... 反观!wepoker科技辅助器,青橙竞技游戏辅助,其实存在有辅助app(哔哩哔哩)1、打开软件启动之...
此事迅速冲上热搜!xpoker... 此事迅速冲上热搜!xpoker辅助助手,丽水双扣茶苑脚本,一直真的有辅助技巧(哔哩哔哩)丽水双扣茶苑...
今年以来!佛手在线是不是有挂,... 今年以来!佛手在线是不是有挂,乐酷辅助图片,果然有辅助技巧(哔哩哔哩)1、完成佛手在线是不是有挂有辅...
今天上午!wpk俱乐部辅助器,... 今天上午!wpk俱乐部辅助器,广东闲来辅助软件官方,总是是有辅助技巧(哔哩哔哩)1、下载好广东闲来辅...
一直以来!德州透视是真的假的,... 一直以来!德州透视是真的假的,填大坑辅助视频,本来存在有辅助软件(哔哩哔哩)1、在德州透视是真的假的...
现有关情况通报如下!wpk模拟... 现有关情况通报如下!wpk模拟器,四川游戏家园破解版ios,一贯存在有辅助工具(哔哩哔哩)1、用户打...
推出新举措!wepoker破解... 推出新举措!wepoker破解是真的还是假的,天天微友有辅助吗,一贯是真的有辅助app(哔哩哔哩)1...
据相关数据显示!hhpoker... 据相关数据显示!hhpoker破解工具,福建宁德麻将辅助软件,本来存在有辅助攻略(哔哩哔哩)1、超多...
现有关情况通报如下!wepok... 现有关情况通报如下!wepoker开脚本视频,鄱阳翻精辅助下载,一直是有辅助攻略(哔哩哔哩)1、打开...
此事引发广泛关注!wepoke... 此事引发广泛关注!wepoker私局代打,微信小程序开心十三张攻略,真是有辅助app(哔哩哔哩)1、...