Java 如何在android studio 3.6.2中为onclick lisner提供设置选项?

Java 如何在android studio 3.6.2中为onclick lisner提供设置选项?,java,android,Java,Android,我更新了我的Android studio版本。在更新的android studio版本中,如何为onclick listener提供设置选项。当我使用onclick listener时,抽屉菜单没有响应 版面截图 这是我的activity类的代码片段 public class HomeActivity extends AppCompatActivity { private AppBarConfiguration mAppBarConfiguration; @Over

我更新了我的Android studio版本。在更新的android studio版本中,如何为onclick listener提供设置选项。当我使用onclick listener时,抽屉菜单没有响应

版面截图

这是我的activity类的代码片段

    public class HomeActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        final FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        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.home, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}
但它不应影响抽屉导航中的其他菜单选项。根据,当您的菜单项被覆盖时,您可以通过覆盖选项项selected()启动特定屏幕:

然后确保菜单项的
android:id
(例如,屏幕截图中的
action\u设置
)与导航图XML中指向设置所用的
的目的地的
android:id
匹配


确保菜单项
android:id
和导航图
android:id
匹配的相同概念适用于所有菜单,包括导航抽屉中使用的菜单(这就是默认模板的
NavigationView
开箱即用的方式)。

您可以在活动中尝试遵循源代码

@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 onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        //item id
        case R.id.logout:

                            //Creating a shared preference

                            SharedPreferences sp = getSharedPreferences(Constant.SHARED_PREF_NAME, Context.MODE_PRIVATE);

                            //Creating editor to store values to shared preferences
                            SharedPreferences.Editor editor = sp.edit();
                            //Adding values to editor
                            editor.putString(Constant.ID_SHARED_PREF, "");

                            //Saving values to editor
                            editor.apply();
                            Intent intent2=new Intent(HomeActivity.this, UserLoginActivity.class);
                            intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent2);

            return true;
        case R.id.settings:
            Intent intent=new Intent(HomeActivity.this, SettingsActivity.class);
            startActivity(intent);
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

我不明白你在工具栏属性工具栏上给点击选项LISNER提供什么设置选项?Android工作室得到了什么?如果你不熟悉模板生成的代码,你可以考虑做。
@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 onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        //item id
        case R.id.logout:

                            //Creating a shared preference

                            SharedPreferences sp = getSharedPreferences(Constant.SHARED_PREF_NAME, Context.MODE_PRIVATE);

                            //Creating editor to store values to shared preferences
                            SharedPreferences.Editor editor = sp.edit();
                            //Adding values to editor
                            editor.putString(Constant.ID_SHARED_PREF, "");

                            //Saving values to editor
                            editor.apply();
                            Intent intent2=new Intent(HomeActivity.this, UserLoginActivity.class);
                            intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent2);

            return true;
        case R.id.settings:
            Intent intent=new Intent(HomeActivity.this, SettingsActivity.class);
            startActivity(intent);
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}