Android Studio Menu Item click not working

后端 未结 1 2067
难免孤独
难免孤独 2021-01-22 14:26

So my menu items wont do anything except show the enlarging animation when clicked on. Im trying to open a new activity with them, I have toast attached to one to see if does an

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-22 14:42

    onOptionsItemSelected is not meant to handle action on BottomNavigationView. Action on MenuItem in BottomNavigationView should be done as follows:

    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;
    
        });
    

    0 讨论(0)
提交回复
热议问题