李剑一 2019-03-17 09:12 采纳率: 0%
浏览 1673

怎样使用item跳转页面?

想要使用item跳转页面
图片说明

比如让item的id为nav_camera的跳转到main.xml,方法怎么写?希望有大佬告知

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
    //HOME页面右上角menu菜单
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        MenuInflater inflater_item = getMenuInflater();
        inflater_item.inflate(R.menu.main, null);
        switch (item.getItemId()){
            case R.id.add_contact:
                Toast.makeText(this, "123", Toast.LENGTH_SHORT).show();
                break;
            case R.id.add_whitelist:
                Toast.makeText(this, "456", Toast.LENGTH_SHORT).show();
                break;
        }

        return super.onOptionsItemSelected(item);
    }

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

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

  • 写回答

2条回答 默认 最新

  • 王能 2019-03-18 09:43
    关注

    看见ifelse就头疼。你的安卓基础不行,不是main.xml而是MainActivity,xml只是相当于配置文件而已,最终还是解析到class里面。
    startActivity(new Intent(this,MainActivity.class))

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿