如何在导航抽屉中添加活动

Posted

技术标签:

【中文标题】如何在导航抽屉中添加活动【英文标题】:How to add activity in Navigation Drawer 【发布时间】:2021-07-05 14:55:06 【问题描述】:

我正在尝试在我的导航抽屉中添加一个活动。我已经在我的主要活动中加入了带有项目 id 的 switch case 的意图。我的应用程序运行完美,但意图没有工作,当我单击我添加意图抽屉关闭但我无法移动到活动的项目时,这是下面的代码。

我打算注册

public class MainActivity extends AppCompatActivity 
DrawerLayout drawerLayout;
NavigationView navigationView;
androidx.appcompat.widget.Toolbar toolbar;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = findViewById(R.id.toolBar);
    setSupportActionBar(toolbar);
    textView = findViewById(R.id.heyTv);
    drawerLayout = findViewById(R.id.drawer_layout);

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,
            R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawerLayout.addDrawerListener(toggle);
    toggle.syncState();

    navigationView = findViewById(R.id.navView);


    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() 
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) 
            switch (item.getItemId())
                case R.id.home:
                    Toast.makeText(MainActivity.this, "Trial", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.profile:
                    Toast.makeText(MainActivity.this, "User", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.logout:
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("Logout")
                            .setIcon(R.drawable.ic_logout)
                            .setMessage("Sure! You want to logout")
                            .setPositiveButton("Yes", new DialogInterface.OnClickListener() 
                                @Override
                                public void onClick(DialogInterface dialog, int which) 
                                    Intent intent = new Intent(MainActivity.this, SigninActivity.class);
                                    startActivity(intent);
                                
                            ).setNegativeButton("No", new DialogInterface.OnClickListener() 
                        @Override
                        public void onClick(DialogInterface dialog, int which) 
                            dialog.dismiss();
                        
                    ).setNeutralButton("Help", new DialogInterface.OnClickListener() 
                        @Override
                        public void onClick(DialogInterface dialog, int which) 
                            Toast.makeText(MainActivity.this, "Press yes to logout", Toast.LENGTH_SHORT).show();
                        
                    ).show();
                    break;
                case R.id.signup:
                    Intent intent = new Intent(MainActivity.this, SignupActivity.class);

                    break;
                case R.id.share:
                    Toast.makeText(MainActivity.this, "Share", Toast.LENGTH_SHORT).show();
                case R.id.mail:
                    Toast.makeText(MainActivity.this, "Contact us", Toast.LENGTH_SHORT).show();
            
            drawerLayout.closeDrawer(GravityCompat.START);
            return true;
        
    );


private void startActivities(Intent intent) 
    startActivities(intent);



@Override
public void onBackPressed() 
    if (drawerLayout.isDrawerOpen(GravityCompat.START))
        drawerLayout.closeDrawer(GravityCompat.START);
    
    else 
        new AlertDialog.Builder(MainActivity.this)
                .setTitle("Exit")
                .setIcon(R.drawable.ic_logout)
                .setMessage("Sure! You want to exit?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() 
                    @Override
                    public void onClick(DialogInterface dialog, int which) 
                        finishAffinity();
                    
                ).setNegativeButton("No", new DialogInterface.OnClickListener() 
            @Override
            public void onClick(DialogInterface dialog, int which) 
                dialog.dismiss();
            
        ).setNeutralButton("Other", new DialogInterface.OnClickListener() 
            @Override
            public void onClick(DialogInterface dialog, int which) 
                Toast.makeText(MainActivity.this, "Press yes to exit the app", Toast.LENGTH_SHORT).show();
            
        ).show();
    

【问题讨论】:

哪个 switch case 不适合你? 注册开关案例 【参考方案1】:

您忘记开始活动了。创建意图后添加一行

Intent intent = new Intent(MainActivity.this, SignupActivity.class);
startActivity(intent);

【讨论】:

以上是关于如何在导航抽屉中添加活动的主要内容,如果未能解决你的问题,请参考以下文章

想要在一项活动中添加底部和侧边导航,但如何?

在导航抽屉上调用不是片段活动的活动

如何从导航抽屉启动活动?

如何使用同一个应用制作抽屉式导航和底部导航?

如何在所有活动中使用Android Studio默认导航抽屉[重复]

如何在另一个活动中从 uri 设置导航抽屉查看器的图像