繁体   English   中英

如何在Kotlin中向菜单项添加单击侦听器

[英]How to add a click listener to Menu Item in Kotlin

我想创建一个底部导航栏。 我使用android BottomNavigationView来创建UI,但我不知道如何在菜单中的项目中添加OnClick监听器。

我在谷歌搜索并找到了一些文章,但他们都使用了工具栏元素。 我不知道如何添加它以及它正在做什么。

这是我在主要活动中包含的导航条形码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:design="http://schemas.android.com/apk/res-auto"
             android:layout_width="match_parent"
             android:layout_height="match_parent">


    <RelativeLayout android:layout_width="match_parent" android:id="@+id/bottom_nav"
                    android:background="@drawable/gradient_theme"
                    android:layout_height="wrap_content"  android:layout_gravity="bottom">

        <android.support.design.widget.BottomNavigationView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/white"
                android:id="@+id/menuBar"
                design:menu="@menu/menu_bar"
                design:itemIconTint="@android:color/darker_gray"/>
    </RelativeLayout>
</FrameLayout>

这是我想要设置监听器的MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        beautifyLayout(this, window)
        setSupportActionBar(toolbar)

        testButton.setOnClickListener {
            val intent= Intent(this,AccountActivity::class.java)
            finish()
            startActivity(intent)
        }
    }
    }

这是menu_bar.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/btn1" android:title="" android:icon="@drawable/ic_feed"/>
    <item android:id="@+id/btn2" android:title="" android:icon="@drawable/ic_chat_bubble_black_24dp"/>
    <item android:id="@+id/btn3" android:title="" android:icon="@drawable/ic_search_black_24dp"/>
    <item android:id="@+id/btn4" android:title="" android:icon="@drawable/ic_menu_black_24dp" />
</menu>

用户界面工作得很好,只需要一些听众。 我希望尽可能只使用BottomNavigationView。

这是你设置监听器的方式:

    val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.btn1 -> {
                // put your code here
                return@OnNavigationItemSelectedListener true
            }
            R.id.btn2 -> {
                // put your code here
                return@OnNavigationItemSelectedListener true
            }
            R.id.btn3 -> {
                // put your code here
                return@OnNavigationItemSelectedListener true
            }
            R.id.btn4 -> {
                // put your code here
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

menuBar.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)

如果menuBar在您的活动布局中,则不需要初始化。
如果没有,则必须使用findViewById()进行初始化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM