简体   繁体   English

Android Studio Navigation Drawer 项打开网站

[英]Android Studio Navigation Drawer item opening website

I used the basic "Navigation Drawer Acticity" in Anroid Studio when creating the new project.创建新项目时,我在 Anroid Studio 中使用了基本的“导航抽屉活动”。 The fragments work well and I've managed to add new fragments and everything.片段运行良好,我设法添加了新片段和所有内容。 However, items in the drawer menu that direct you to a certain website don't work.但是,抽屉菜单中将您引导至某个网站的项目不起作用。 I've tried different methods with public boolean onNavigationItemSelected(MenuItem item){} and nothing happens when I press the items.我用 public boolean onNavigationItemSelected(MenuItem item){} 尝试了不同的方法,当我按下这些项目时没有任何反应。

It looks like this:它看起来像这样:

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {

        int id = item.getItemId();
        

       if (id == R.id.nav_moodle) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
            startActivity(browserIntent);

            return true;
        }

        return true;
    }

As you can see I did nothing for the fragments to work since they work fine already but when I press the button "moodle", nothing happens.正如你所看到的,我没有为片段工作做任何事情,因为它们已经工作正常但是当我按下按钮“moodle”时,什么也没有发生。

What have I done wrong?我做错了什么?

Here is my MainActivity:这是我的主要活动:

package com.example.ustudy;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import android.widget.Toast;

import com.example.ustudy.ui.home.HomeFragment;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;

import androidx.annotation.NonNull;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;

import com.example.ustudy.databinding.ActivityMainBinding;

import org.jetbrains.annotations.NotNull;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{

    private AppBarConfiguration mAppBarConfiguration;
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());




        //required for the top bar to be clickable
        setSupportActionBar(binding.appBarMain.toolbar);

        //button in the bottom right corner
        binding.appBarMain.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 = binding.drawerLayout;
        //Navigationview for the drawer (implemented in "activity_main.xml")
        NavigationView navigationView = binding.navView;
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.

        //each screen mentioned in the columns below (R.id.xy) will show the hamburger menu in the top left corner
        //on screens, not included in the columns, there will be a "back arrow" instead of the ham. menu
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_nachricht, R.id.nav_lerngruppe, R.id.nav_lehrveranstaltung)
                .setDrawerLayout(drawer)
                .build();

        //controls what happens when one of the items in the hamburger menu is clicked on
        //go to "mobile_navigation.xml" to add screens to go to
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);

        //required to set up the hamburger menu icon and the back-arrow icon on the top bar(without it
        //the user had to slide the side of the screen to open the menu)
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);

        //enables the items in the hamburger menu to go to a certain screen after clicking on them
        NavigationUI.setupWithNavController(navigationView, navController);




    }

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

    @Override
    public boolean onSupportNavigateUp() {
        //enables the usage of the back-arrow on every screen
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }


    @Override
    public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.nav_moodle) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
            startActivity(browserIntent);
            return true;
        }
        return true;
    }
}

Please, let me know if you need more!如果您需要更多,请告诉我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM