gpt4 book ai didi

android-resources - Lint 检查潜在的复数形式?

转载 作者:行者123 更新时间:2023-12-04 13:07:54 26 4
gpt4 key购买 nike

我的 Lintcheck 提示我的字符串资源。

Potential Plurals res/values/strings.xml Formatting %d followed by words ("Pers"): This should probably be a plural rather than a string



此资源以前是 ,但我将其更改为普通字符串,例如:
<string name="plain_string">%d Pers</string>

我会理解警告,但错误?我该如何解决这个问题?

最佳答案

将字符串转换为复数形式,如描述 here :

<plurals name="plain_string">
<item quantity="one">%d Pers</item>
<item quantity="other">%d Pers</item>
</plurals>
在您的代码中,您必须替换
getContext().getString(R.string.plain_string, pers)
经过
getContext().getResources().getQuantityString(R.plurals.plain_string, pers, pers)
或者只是抑制这样的警告:
<string name="plain_string" tools:ignore="PluralsCandidate">%d Pers</string>
Kotlin 扩展处理 null 并自动传入带有格式的字符串的计数:
// note: these methods won't work if you have multiple formats in your string

// plural strings without formatted count ("book", "books")
fun Context.getQuantityString(resId: Int, maybeCount: Int?, defaultCount: Int = 0): String? {
val count = maybeCount ?: defaultCount
return resources?.getQuantityString(resId, count, count)
}

// plural strings with formatted count (e.g. "%d book", "%d books")
fun Context.getPluralString(resId: Int, maybeCount: Int?, defaultCount: Int = 0): String? {
return resources?.getQuantityString(resId, maybeCount ?: defaultCount)
}

// usage
context?.getQuantityString(R.plurals.words_count, nullableCount)

关于android-resources - Lint 检查潜在的复数形式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47439853/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com