无法在导航抽屉按钮单击上显示吐司

时间:2021-05-25 00:03:29

标签: android navigation-drawer buttonclick

目标: 我想在导航抽屉按钮单击时显示祝酒词。它显示在我在屏幕上添加的浮动操作按钮上。

我的问题:

我无法通过单击导航抽屉中的按钮来显示它。

这是我的MainActivity.java

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    private DrawerLayout drawerLayout;

    @SuppressLint({"SetJavaScriptEnabled"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this,"Hello", Toast.LENGTH_LONG).show();
            }
        });


        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);


    }

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

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this,                                    R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }



    @SuppressLint({"SetJavaScriptEnabled", "NonConstantResourceId"})
    @Override
    public boolean onOptionsItemSelected(MenuItem item){

        switch (item.getItemId()) {
        case R.id.nav_share:

            Toast.makeText(MainActivity.this,"Hello", Toast.LENGTH_LONG).show();

        default:

            return super.onOptionsItemSelected(item);

    }

}


这是我的activity_main_drawer.xml。它总共有 2 个组和 5 个项目,我想在他们点击时显示敬酒。

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

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/ic_baseline_home_24"
            android:title="@string/menu_home" />
        <item
            android:id="@+id/nav_gallery"
            android:icon="@drawable/ic_menu_gallery"
            android:title="@string/menu_gallery" />
        <item
            android:id="@+id/nav_slideshow"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="@string/menu_slideshow" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_share"
                android:title="Share the app" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_send"
                android:title="Send" />
        </menu>
    </item>
</menu>

2 个答案:

答案 0 :(得分:3)

您可能需要为 Navigation Drawer 项目设置一个 onClick 事件。 将实现 NavigationView.OnNavigationItemSelectedListener 添加到您的 MainActivity。

参考 Grzegorz 的回答 onClick event in navigation drawer

答案 1 :(得分:0)

开发人员文档建议您只使用 NavigationView 而不是 AppCompactActivity 的 onOptionsItemSelected() 函数。为了让你的 toast 工作,你需要覆盖 onNavDestinationSelected(),让它做它的默认行为加上 toast。

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return navigationView.onNavDestinationSelected(item, navController)
                || super.onOptionsItemSelected(item);
    }
    

https://developer.android.com/guide/navigation/navigation-ui#Tie-navdrawer