代码之家  ›  专栏  ›  技术社区  ›  AVEbrahimi

如果在Kotlin中,移除额外的

  •  0
  • AVEbrahimi  · 技术社区  · 7 年前

    我怎样才能卸下这个箱子 如果(savedInstanceState!=null)

    override fun onViewStateRestored(savedInstanceState: Bundle?) {
            super.onViewStateRestored(savedInstanceState)
            if (savedInstanceState != null)
                search_bar.visibility =
                        if (savedInstanceState.getBoolean("showSearchBar", false)) View.VISIBLE else View.GONE
        }
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   arekolek    7 年前

    你可以使用安全通道 ?. 在…上 savedInstanceState 和 compare nullable Boolean to true :

    val showSearchBar = savedInstanceState?.getBoolean("showSearchBar", false) == true
    search_bar.visibility = if (showSearchBar) View.VISIBLE else View.GONE
    

    注意:这将隐藏搜索栏,即使在 倚仗 false getBoolean 无论如何

    顺便说一句 Android KTX 有一个 View.isVisible

    search_bar.isVisible =
        savedInstanceState?.getBoolean("showSearchBar", false) == true