如何通过底部导航开始新活动

时间:2019-01-20 16:09:25

标签: android

我想通过底部导航栏开始新活动,但是它不起作用。

我也检查了许多教程,但是不知道问题出在哪里。

这是我的代码。

public class display extends AppCompatActivity {

    private TextView mTextMessage;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    Intent i = new Intent(display.this, Addab.class);
                    startActivity(i);
                    break;

                case R.id.navigation_dashboard:
                    mTextMessage.setText(R.string.title_dashboard);
                    break;
                case R.id.navigation_notifications:
                    mTextMessage.setText(R.string.title_notifications);
                    break;
            }

            return false;
        }
    };

        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);

        mTextMessage = (TextView) findViewById(R.id.message);
        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

    }

}

2 个答案:

答案 0 :(得分:0)

使用break;代替return true;

答案 1 :(得分:0)

@Override
public boolean onNavigationItemSelected(@NonNull final MenuItem item) {
   switch (item.getItemId()) {
       case R.id.navigation_home:
    startActivity(new Intent(this, Abc.class));
           Break;
       case R.id.navigation_camera:
           startActivity(new Intent(this, Cab.class));
           Break;

   }
   finish();
   return true;
}

也请参考这里-> https://blog.fossasia.org/using-activities-with-bottom-navigation-view-in-phimpme-android/