You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio无法识别字符串资源注解中的key与value参数求助

解决String资源中Spannable注解的key/value无法被Android Studio识别的问题

我在处理String资源内的Spannable字符串时,遇到Android Studio无法识别<annotation>标签的key和value的问题,相关代码及问题截图如下:

字符串资源代码

<string name="terms_conditions_prompt" formatted="true">Click to accept the <annotation action="openTC">terms and conditions</annotation></string>

Kotlin业务代码

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
        )
    }
}

问题截图

问题截图

解决方法

  • 同步Gradle项目:点击顶部菜单栏File > Sync Project with Gradle Files,刷新资源索引。
  • 清理重建项目:依次执行Build > Clean Project和Build > Rebuild Project,清除编译缓存。
  • 校验formatted属性:确保字符串资源的formatted="true",该属性是系统保留注解Span信息的必要条件。
  • 确认Annotation类导入:代码中需导入android.text.Annotation,避免误用其他同名类。
  • 重启Android Studio:若IDE缓存异常,重启后通常能恢复正常识别。

内容的提问来源于stack exchange,提问作者maresan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 01:07:19