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怎么增加运气,hhpoker必备开挂,一贯真的有挂(今日头条)1)有没有挂:...
推荐攻略!hhpoker德州有... 推荐攻略!hhpoker德州有挂吗,德普之星私人局透视,都是是真的有挂(证实有挂)1、起透看视 辅助...
科普!wepoker怎么提高运... 科普!wepoker怎么提高运气,拱趴大菠萝有什么挂,一贯是有挂(今日头条)1、不需要AI权限,帮助...
盘点一款!wepoker辅助软... 盘点一款!wepoker辅助软件视频,来玩app破解,都是真的有挂(有挂解惑)一、wepoker辅助...
推荐十款!hhpoker是真的... 推荐十款!hhpoker是真的假的,wepoker怎么提高运气,本来真的有挂(有挂详情)脚本下载中分...
分享个大家!德扑圈有透视吗,微... 分享个大家!德扑圈有透视吗,微扑克透视脚本,本来有挂(有挂总结)1)免费钻石:进一步探索免费脚本大陆...
我来教大家!wpk辅助,wep... 我来教大家!wpk辅助,wepoker透视有没有,好像是真的有挂(真是有挂)1、上手简单,内置详细流...
实测揭晓!hhpoker德州挂... 实测揭晓!hhpoker德州挂真的有吗,福建大菠萝万能辅助器,好像是有挂(讲解有挂)1、下载好正确养...
科普攻略!wpk插件辅助,we... 科普攻略!wpk插件辅助,wepoker透视功能下载,确实有挂(揭秘有挂)透视方法中分为三种模型:靠...
揭秘关于!pokemmo手机版... 揭秘关于!pokemmo手机版脚本免费,wpk私人局辅助是真的吗,好像存在有挂(确实有挂)1)有没有...