我是应用程序构建中的菜鸟,但我正在尝试! 我已经建立了应用程序的基础,首先在android studio中尝试。 构建一个包含片段的菜单。 但是我希望有一个菜单项链接到一个活动(因为我希望它看起来与其他活动不同。) 尝试了很多,但它不起作用。 你帮帮我吗?
这是我的菜单代码:
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_camera) {
MainFragment fragment = new MainFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_gallery) {
Zakkaartjes fragment = new Zakkaartjes();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_slideshow) {
Rekenen fragment = new Rekenen();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_manage) {
//THIS IS WHERE I WOULD LIKE TO HAVE A LINK TO MY "notities activity"
} else if (id == R.id.nav_share) {
Protocollen fragment = new Protocollen();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_send) {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","info@verpleegkundigzakboek.nl", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email van verpleegkundig zakboek app");
startActivity(Intent.createChooser(emailIntent, "Verzend e-mail"));
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Made Notities.java(其中包含其他布局)并制作了activity_notities.xml 希望有人能帮助我吗?!
答案 0 :(得分:0)
如果我理解正确,这应该有效:
else if (id == R.id.nav_manage) {
Intent intent = new Intent(this, YourNextActivity.class);
startActivity(intent);
finish();
}
此外,您可以考虑使用switch
代替大量else if
:
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getId()){
case R.id.nav_camera:
//your code
break;
case R.id.nav_gallery:
//your code
break;
// add case blocks for each id you want to manage
}
}
有关switch
here的更多信息。