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)

相关内容

热门资讯

WPK德州局窍要!wepoke... WPK德州局窍要!wepoker辅助器怎么用,德州微扑克好像是真的有透视挂方法1、wepoker辅助...
WPK下载窍门!aapoker... WPK下载窍门!aapoker辅助挂,德州微扑克原来是真的有开挂插件aapoker辅助挂能透视中分为...
WPK透视大纲!wepoker... WPK透视大纲!wepokerplus外挂,德州微扑克本来有辅助app1、下载好wepokerplu...
WPK安装策略!wepoker... WPK安装策略!wepoker透视脚本免费,德州微扑克都是是真的有开挂方法1、许多玩家不知道wepo...
WPK私人局要领!wepoke... WPK私人局要领!wepoker私人辅助器,德州微扑克竟然有透视工具1、许多玩家不知道wepoker...
WPK德州局诀窍!WePOke... WPK德州局诀窍!WePOker有没有透视方法,德州微扑克其实真的有透视app在进入WePOker有...
WPK有透视资料!we-pok... WPK有透视资料!we-poker辅助器,德州微扑克原来存在有开挂攻略1、操作简单,无需we-pok...
WPK透视操作!wepoker... WPK透视操作!wepoker透视脚本视频,德州微扑克一贯存在有辅助器工具1、该软件可以轻松地帮助玩...
WPK脚本窍门!wepoker... WPK脚本窍门!wepoker黑侠破解,德州微扑克好像是真的有辅助app1、操作简单,无需wepok...
WPK作弊手筋!wepoker... 您好,wepoker怎么设置房间这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...