Android Studio菜单项单击不起作用

因此,我的菜单项除了单击时会显示放大的动画外,什么也不会做.我正在尝试与他们开展一项新活动,我敬酒意味是否可以做什么,但我一无所获.这是常见问题吗?

minSdkVersion 17
targetSdkVersion 27

布局

<android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

该布局正在使用android.support.constraint.ConstraintLayout

菜单

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/home"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_apps"
        android:icon="@drawable/apps"
        android:title="@string/title_apps" />

    <item
        android:id="@+id/navigation_profile"
        android:icon="@drawable/profile"
        android:title="@string/title_profile" />

    <item
        android:id="@+id/navigation_logout"
        android:icon="@drawable/logout"
        android:title="@string/title_logout" />

</menu>

Java的

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.navigation, menu);
        return true;
    }

@Override
public boolean onoptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement

    if (id == R.id.navigation_home) {

        Intent navHome = new Intent(this, wordpressActivity.class);
        this.startActivity(navHome);
        return true;
    }

    if (id == R.id.navigation_apps) {
        Toast.makeText(this, "apps is Clicked", Toast.LENGTH_LONG).show();
        return true;
    }

    if (id == R.id.navigation_profile) {
        Intent navProf = new Intent(this, AccountActivity.class);
        this.startActivity(navProf);
        return true;
    }

    if (id == R.id.navigation_logout) {
        Intent navLog = new Intent(this, logoutActivity.class);
        this.startActivity(navLog);
        return true;
    }

    return super.onoptionsItemSelected(item);
}

预先感谢您对问题的任何帮助或指示.我对使用菜单项“点击意图”是陌生的,我真的不明白为什么它不起作用.

解决方法:

onoptionsItemSelected并不意味着处理BottomNavigationView上的操作. BottomNavigationView中对MenuItem的操作应按以下步骤进行:

BottomNavigationView bottomNavigationView = findViewById(R.id.navigation);
bottomNavigationView.setonNavigationItemSelectedListener(new BottomNavigationView
            .OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement

            if (id == R.id.navigation_home) {

                Intent navHome = new Intent(MainActivity.this, wordpressActivity.class);
                MainActivity.this.startActivity(navHome);
                return true;
            }

            if (id == R.id.navigation_apps) {
                Toast.makeText(MainActivity.this, "apps is Clicked", Toast.LENGTH_LONG).show();
                return true;
            }

            if (id == R.id.navigation_profile) {
                Intent navProf = new Intent(MainActivity.this, AccountActivity.class);
                MainActivity.this.startActivity(navProf);
                return true;
            }

            if (id == R.id.navigation_logout) {
                Intent navLog = new Intent(MainActivity.this, logoutActivity.class);
                MainActivity.this.startActivity(navLog);
                return true;
            }
            return false;

    });

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...