首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用kotlin在菜单中制作可点击的链接

使用kotlin在菜单中制作可点击的链接
EN

Stack Overflow用户
提问于 2018-06-04 01:24:00
回答 2查看 125关注 0票数 0

我试着做一个底部导航,我有这个代码

代码语言:javascript
复制
val bottomNavigation = findViewById<View>(R.id.bottom_navigation) as 
  BottomNavigationView
    bottomNavigation.setOnNavigationItemSelectedListener { item ->
        when (item.itemId) {

            R.id.botom__nav__home ->
                // Action when tab 1 selected
                val intent = Intent(this, HomeActivity::class.java)
            R.id.botom__nav__profile ->
                // Action when tab 2 selected
                val intent = Intent(this, LikeActivity::class.java)
            else ->
                // Action when tab 3 selected
                val intent = Intent(this, ProfileActivity::class.java)
        }
        true
    }
    startActivityForResult(intent, 99)
}

我有以下错误:

代码语言:javascript
复制
' Expecting an expression '
' Expecting "->" '

对于“When”中的每个元素...

有人能帮我修复这些错误吗?

EN

回答 2

Stack Overflow用户

发布于 2018-06-04 01:50:50

问题是,您在when块中多次声明val intent。要解决这个问题,只需将intent的声明移到您的when块之外,例如:

代码语言:javascript
复制
lateinit var intent:Intent
bottomNavigation.setOnNavigationItemSelectedListener { item ->
    when (item.itemId) {

        R.id.botom__nav__home ->
            // Action when tab 1 selected
            intent = Intent(this, HomeActivity::class.java)
        R.id.botom__nav__profile ->
            // Action when tab 2 selected
            intent = Intent(this, LikeActivity::class.java)
        else ->
            // Action when tab 3 selected
            intent = Intent(this, ProfileActivity::class.java)
    }
    true
}
startActivityForResult(intent, 99)
票数 1
EN

Stack Overflow用户

发布于 2018-06-04 01:45:04

请删除此行中的item ->

代码语言:javascript
复制
bottomNavigation.setOnNavigationItemSelectedListener { item ->
    when (item.itemId) {` 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50669071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档