我正在使用字符串资源中的 Spannable 字符串,但是当我尝试获取我的键和值时,注释无法识别这个字符串。
<string name="terms_conditions_prompt" formatted="true">Click to accept the <annotation action="openTC">terms and conditions</annotation></string>
val termsText = getText(R.string.terms_conditions_prompt) as SpannedString
val annotations = termsText.getSpans(0, termsText.length, Annotation::class.java)
val termsCopy = SpannableString(termsText)
for (annotation in annotations) {
if (annotation.key == "action") {
termsCopy.setSpan(
createClickSpan(annotation.value),
termsText.getSpanStart(annotation),
termsText.getSpanEnd(annotation),
SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
}
您可能只是使用了错误的
Annotation
类导入,这就是为什么找不到 key
和 value
属性,因为导入的类没有这些。
确保您正在使用
import android.text.Annotation
。