为什么android studio中的菜单项无法点击?

时间:2017-12-29 22:59:25

标签: android android-layout android-menu

我正在开发一个关于android的项目,对此我是全新的。我用一些菜单项做了一个抽屉菜单。现在,当我想在菜单项上点击另一个活动时,抽屉自动关闭,它不起作用。在这里

private DrawerLayout mdrawerLayout;
private ActionBarDrawerToggle mdrawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mdrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    mdrawerToggle = new ActionBarDrawerToggle(this, mdrawerLayout, R.string.open, R.string.close);
    mdrawerLayout.addDrawerListener(mdrawerToggle);

    mdrawerToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mdrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    int id = item.getItemId();
    if(id == R.id.setup){
        Intent csetup = new Intent(MainActivity.this, Subactivity.class);
        startActivity(csetup);

        return false;

    }
    return super.onOptionsItemSelected(item);
}

} `

3 个答案:

答案 0 :(得分:0)

这是因为你重写了错误的方法。您必须覆盖抽屉的onNavigationItemSelected(),而不是onOptionsItemSelected(),这是用于右上角设置菜单。请参阅以下代码以供参考:

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.setup) {
             Intent csetup = new Intent(MainActivity.this, Subactivity.class);
             startActivity(csetup);
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

答案 1 :(得分:0)

首先使用NavigationView方法为您findViewById查找视图,如下所示 - NavigationView navView=(NavigationView)findViewById(R.id.navView) 然后使用它。

navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            int id = item.getItemId();

            if (id == R.id.setup) {
                Intent csetup = new Intent(MainActivity.this, Subactivity.class);
                startActivity(csetup);
            }

            return false;

        }
    });

然后它就可以了。onOptionsItemSelected(MenuItem item)用于访问我们在工具栏或其他布局上使用MenuItems方法充气的onCreateOptionsMenu()

答案 2 :(得分:0)

试试这个: 在onCreate方法中添加以下代码:

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

在mainactivity类中实现lister:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener

然后在你的onNavigationItemSelected方法中看起来像这样:

   @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.setup) {
        //call new activity
         }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
     }

希望这可以帮到你