java - 当 WebView 中加载特定 URL 时隐藏菜单项

标签 java android html webview menu

我有一个带有基本 WebView 的 Activity 。其中,我有一个菜单项,可以启动共享 Intent 并允许用户共享他们当时正在浏览的 URL。我还有一个错误 HTML 页面,当 WebView 收到任何 HTTP 错误时会加载该页面。因此,基本上,我希望在加载该错误文档时隐藏“共享”菜单项(或者至少共享我可以设置的其他一些 URL)。

这是我的 MainActiviity.java:

package com.ananya.brokenhearts;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;

@SuppressWarnings("deprecation")

public class MainActivity extends AppCompatActivity
{
    private WebView WebView;
    private ProgressBar ProgressBar;
    private LinearLayout LinearLayout;
    private String currentURL;

    @SuppressLint("SetJavaScriptEnabled")

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView = findViewById(R.id.webView);
        ProgressBar = findViewById(R.id.progressBar);
        LinearLayout = findViewById(R.id.layout);

        ProgressBar.setMax(100);

        WebView.loadUrl("https://www.brokenhearts.ml/index.html");
        WebView.getSettings().setJavaScriptEnabled(true);
        WebView.setWebViewClient(new WebViewClient()
        {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon)
            {
                LinearLayout.setVisibility(View.VISIBLE);
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url)
            {
                LinearLayout.setVisibility(View.GONE);
                super.onPageFinished(view, url);
                currentURL = url;
            }

            public void onReceivedError(WebView webview, int i, String s, String s1)
            {
                WebView.loadUrl("file:///android_asset/error.html");
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url2)
            {
                if (url2.contains("www.brokenhearts.ml"))
                {
                    view.loadUrl(url2);
                    return false;
                } else
                {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url2));
                    startActivity(intent);
                    return true;
                }
            }
        });

        WebView.setWebChromeClient(new WebChromeClient()
        {
            @Override
            public void onProgressChanged(WebView view, int newProgress)
            {
                super.onProgressChanged(view, newProgress);
                ProgressBar.setProgress(newProgress);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.backward:
                onBackPressed();
                break;

            case R.id.forward:
                onForwardPressed();
                break;

            case R.id.refresh:
                WebView.reload();
                break;

            case R.id.share:
                Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("text/plain");
                shareIntent.putExtra(Intent.EXTRA_TEXT,currentURL);
                shareIntent.putExtra(Intent.EXTRA_SUBJECT,"Copied URL");
                startActivity(Intent.createChooser(shareIntent,"Share URL"));
                break;

            case R.id.exit:
                new AlertDialog.Builder(this,R.style.AlertDialog)
                        .setIcon(R.drawable.ic_error_black_24dp)
                        .setTitle("Are you sure you want to exit?")
                        .setMessage("Tapping 'Yes' will close the app. Tap 'No' to continue using the app")
                        .setPositiveButton("Yes",
                                new DialogInterface.OnClickListener()
                                {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which)
                                    {
                                        finish();
                                    }
                                })
                        .setNegativeButton("No", null)
                        .show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void onForwardPressed()
    {
        if (WebView.canGoForward())
        {
            WebView.goForward();
        } else
        {
            Toast.makeText(this, "Can't go further", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onBackPressed ()
    {
        if (WebView.canGoBack())
        {
            WebView.goBack();
        } else
        {
            new AlertDialog.Builder(this,R.style.AlertDialog)
                    .setIcon(R.drawable.ic_error_black_24dp)
                    .setTitle("Are you sure you want to exit?")
                    .setMessage("Tapping 'Yes' will close the app. Tap 'No' to continue using the app")
                    .setPositiveButton("Yes",
                            new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface dialog, int which)
                                {
                                    finish();
                                }
                            })
                    .setNegativeButton("No", null)
                    .show();
        }
    }
}

最佳答案

关于隐藏共享菜单图标,如果出现错误标志,您始终可以使用 onPrepareOptionsMenu() 来隐藏共享菜单。

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    menu.findItem(R.id.share).setVisible(!isPageError);
    return true;
}

这需要您在

中将 isPageError boolean 值设置为 true
@Override
public void onReceivedError(WebView webview, int i, String s, String s1{
    isPageError = true;
}

关于java - 当 WebView 中加载特定 URL 时隐藏菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51224366/

相关文章:

java - 在运行时更改十六进制字符串

javascript - 如何使用 jquery/javascript 在 html 选择选项中填充 javascript 对象值的数据

html - nth-child 具有不同宽度的 CSS 网格布局

java - 记事本程序中的复制按钮

android - 如何在 Firebase 中自定义子 key ?

android - 无论应用程序是否运行,在 Android O 上启动服务

javascript - 如何始终保持div相同的高度

java - 单独且不连续的 JTable 单元格选择

连接到 rfcomm0 的 Java rxtx 代码不起作用

java - 引用计数是一个好的设计吗