programmatically add id to R.id

后端 未结 3 600
庸人自扰
庸人自扰 2020-11-28 05:08

I am creating an EditText object which I then try to reference in a unit test. What is the best way to add a new id to R.id for this d

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 05:30

    Here's my solution as an extension function in kotlin:

    /* sets a valid id that isn't in use */
    fun View.findAndSetFirstValidId() {
        var i: Int
        do {
            i = Random.nextInt()
        } while (findViewById(i) != null)
        id = i
    }
    

提交回复
热议问题