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

Android Studio Kotlin中CheckBox文本修改后不生效的问题求助

解决CheckBox文本不更新问题

从你提供的activity_main.xml来看,CheckBox已经配置了android:text="Milliseconds",但运行后仍显示默认文本,同时出现警告,可按以下步骤排查解决:

  • 检查代码是否动态覆盖了文本
    打开MainActivity.kt,确认有没有类似这样的代码:

    val checkBox = findViewById<CheckBox>(R.id.checkBox)
    checkBox.text = "CheckBox"
    

    如果存在,删除该行或修改为目标文本。

  • 清理项目缓存
    点击Android Studio顶部菜单File -> Invalidate Caches...,选择Invalidate and Restart,重启后重新编译运行。

  • 用字符串资源消除警告并规范配置
    你看到的警告源于硬编码文本,Android推荐使用字符串资源管理文本内容:

    1. 打开res/values/strings.xml,添加:
      <string name="milliseconds_label">Milliseconds</string>
      
    2. 修改CheckBox的text属性:
      android:text="@string/milliseconds_label"
      
  • 确认布局文件关联正确
    检查MainActivity.kt中的setContentView方法,确保加载的是R.layout.activity_main,避免加载了错误的布局文件。

CheckBox显示异常截图

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 17:33:14