【问题标题】:Why does Android Studio dsplay Hint of MutableState<Sections> as Sections?为什么 Android Studio 将 MutableState<Sections> 的提示显示为 Sections?
【发布时间】:2021-10-26 01:21:31
【问题描述】:

我使用 Android Studio Hint 来显示变量的类型。

在代码A中,a的Hint显示为MutableState&lt;Sections&gt;,可以看到图片A。

currentSection的Hint显示为Sections,可以看图B,我觉得应该是MutableState&lt;Sections&gt;吧?

代码 A

   val a = rememberSaveable { mutableStateOf(tabContent.first().section)}

   val (currentSection, updateSection) = rememberSaveable {       
        mutableStateOf(tabContent.first().section)
    }

图片A

图片 B

【问题讨论】:

    标签: android kotlin android-jetpack-compose


    【解决方案1】:

    acurrentSection 应具有不同的数据类型。

    currentSection 是通过使用解构声明

    来赋值的
    val (currentSection, updateSection) = rememberSaveable {
        ...
    }
    

    在 jetpack compose 中,您可以通过多种方式创建状态

    • val mutableState = 记住 { mutableStateOf(default) }
    • 记住变量值 { mutableStateOf(default) }
    • val (value, setValue) = 记住 { mutableStateOf(default) }

    您也可以签入documentation

    【讨论】:

    • 谢谢!我觉得acurrentSection都是状态,为什么Android Studio的Hint不一样?
    • 提示显示acurrentSection的数据类型,两者的数据类型不同
    猜你喜欢
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    相关资源
    最近更新 更多