我如何做到这一点,所以当我单击菜单时它会执行某些操作(在这种情况下,请写“ click”)?由于某种原因,它不起作用。我关注了一个youtube视频,这是android studio的新手。先感谢您。另外,我该如何绕过这个“大部分是代码”错误。当我没有更多的解释。
DrawerLayout drawerLayout;
ActionBarDrawerToggle drawerToggle;
Toolbar toolbar;
NavigationView navigationView;
private DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawer);
drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open_drawer,R.string.close_drawer);
drawerLayout.addDrawerListener(drawerToggle);
drawerToggle.setDrawerIndicatorEnabled(true);
drawerToggle.syncState();
navigationView = findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
}
OnClick部分
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId())
{
case R.id.nav_new_purchase:
Toast.makeText(this,"CLICK",Toast.LENGTH_LONG).show();
break;
case R.id.nav_active_purchase:
//TODO
case R.id.nav_old_purchase:
//TODO
}
return false;
}
带有(main_activity.xml)的XML文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/drawer">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/app_toolbar"/>
<com.google.android.material.navigation.NavigationView
android:id="@+id/navigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/drawer_menu"
android:layout_gravity="start">
</com.google.android.material.navigation.NavigationView>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:foreground="@drawable/ic_action_add"
app:backgroundTint="#00FF00"
app:fabCustomSize="200dp"
app:fabSize="auto"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:text="@string/add"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/add_button" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>
答案 0 :(得分:-1)
您检查了以下内容:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
//add this line
Toast.makeText(this,"WORK",Toast.LENGTH_LONG).show();
switch (menuItem.getItemId())
{
case R.id.nav_new_purchase:
Toast.makeText(this,"nav_new_purchase",Toast.LENGTH_LONG).show();
break;
case R.id.nav_active_purchase:
Toast.makeText(this,"nav_new_purchase",Toast.LENGTH_LONG).show();
break;
case R.id.nav_old_purchase:
Toast.makeText(this,"nav_new_purchase",Toast.LENGTH_LONG).show();
break;
default:
Toast.makeText(this,"Not found menu id",Toast.LENGTH_LONG).show();
break;
}
return false;
}
如果显示“ WORK”,则表示您对工作进行编码。此外,还会显示单击的ID或“找不到菜单ID”。如果将显示“未找到菜单ID”,则您检查了错误的菜单ID。
答案 1 :(得分:-1)
drawerLayout.closeDrawer(GravityCompat.START)在最后一行单击侦听器中添加此代码