簡體   English   中英

Android Studio Navigation Drawer 項打開網站

[英]Android Studio Navigation Drawer item opening website

創建新項目時,我在 Anroid Studio 中使用了基本的“導航抽屜活動”。 片段運行良好,我設法添加了新片段和所有內容。 但是,抽屜菜單中將您引導至某個網站的項目不起作用。 我用 public boolean onNavigationItemSelected(MenuItem item){} 嘗試了不同的方法,當我按下這些項目時沒有任何反應。

它看起來像這樣:

    @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;
    }

正如你所看到的,我沒有為片段工作做任何事情,因為它們已經工作正常但是當我按下按鈕“moodle”時,什么也沒有發生。

我做錯了什么?

這是我的主要活動:

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;
    }
}

如果您需要更多,請告訴我!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM