Android 在扩展函数中格式化字符串

Android Formatting String in Extension Function

我试图通过使用以下扩展函数在 AndroidViewModel 中获取 Formatting String 但失败了:

<string name="version_text">Version %1$s</string>

val versionText = getString(R.string.version_text, "1.0.0")

fun AndroidViewModel.getString(resId: Int, vararg formatArgs: Any) = (getApplication() as Context).getString(resId, formatArgs)

结果:

Version [Ljava.lang.Object;@bad21e0

vararg 参数是 Array 类型。使用 * 运算符展开它们:

(getApplication() as Context).getString(resId, *formatArgs)