Cannot resolve string supplied to vararg parameter in extension function

后端 未结 2 1258
遇见更好的自我
遇见更好的自我 2021-01-02 08:26

strings.xml

Showing your number: %1$s

ActivityExt.kt

2条回答
  •  無奈伤痛
    2021-01-02 09:13

    Use the spread operator:

    fun Activity.showToast(textResId: Int, vararg formatArgs: String) {
        val text = getString(textResId, *formatArgs)
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
    }
    

    Currently, you're passing an array as the format argument. By using the spread operator you pass the contents of the array as the format argument.

提交回复
热议问题