我想知道在Android Studio WebView上是否有可能禁用页面上的一些链接(Telegram Instagram电子邮件)

x4shl7ld  于 2023-04-04  发布在  Android
关注(0)|答案(4)|浏览(127)

简而言之,我希望当我点击电报在webview我不希望链接打开,或者如果我点击电报我想留在同一页面上没有链接打开
我试着搜索,但我没有找到任何东西,最多我找到了一些关于如何禁用所有链接的webview,而不是一些

6jygbczu

6jygbczu1#

你应该使用webview客户端拦截点击。

编码:

在您的webview集合webViewClient中

webView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            String url = request.getUrl().toString();
            if (isValidEmail(url)) {
                return true;
            } else if (url.contains("www.instagram.com"))
                return true;
            else if (url.contains("https://t.me"))
                return true;
            else
                return super.shouldOverrideUrlLoading(view, request)
        }

        @Override
        public void onLoadResource(WebView view, String url) {
            // do nothing
            //super.onLoadResource(view, url);
        }
    });

检查是否为有效邮件:

public static boolean isValidEmail(CharSequence target) {     return (!TextUtils.isEmpty(target) && Patterns.EMAIL_ADDRESS.matcher(target).matches());
 }

现在在shouldOverrideUrlLoading中你可以检查url并根据你的需要返回是否覆盖url加载。

ldfqzlk8

ldfqzlk82#

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

    webView = (WebView) findViewById(R.id.webview);
    
    webView.setWebViewClient(new WebViewClient());
    

    webView.setWebChromeClient(new MyChrome());
    webView.loadUrl("https://streamingcommunity.love/");

    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);

}

@Override
public void onBackPressed() {
    if (webView.canGoBack()) {
        webView.goBack();
    } else {
        super.onBackPressed();
    }
}

private class MyChrome extends WebChromeClient {

    private View mCustomView;
    private WebChromeClient.CustomViewCallback mCustomViewCallback;
    protected FrameLayout mFullscreenContainer;
    private int mOriginalOrientation;
    private int mOriginalSystemUiVisibility;

    MyChrome() {}

    public Bitmap getDefaultVideoPoster()
    {
        if (mCustomView == null) {
            return null;
        }
        return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
    }

    public void onHideCustomView()
    {
        ((FrameLayout)getWindow().getDecorView()).removeView(this.mCustomView);
        this.mCustomView = null;
        getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
        setRequestedOrientation(this.mOriginalOrientation);
        this.mCustomViewCallback.onCustomViewHidden();
        this.mCustomViewCallback = null;
    }

    public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback)
    {
        if (this.mCustomView != null)
        {
            onHideCustomView();
            return;
        }
        this.mCustomView = paramView;
        this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
        this.mOriginalOrientation = getRequestedOrientation();
        this.mCustomViewCallback = paramCustomViewCallback;
        ((FrameLayout)getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
        getWindow().getDecorView().setSystemUiVisibility(3846 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    }
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    webView.saveState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    webView.restoreState(savedInstanceState);
}

}

wixjitnu

wixjitnu3#

WebView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view,WebResourceRequest request){ String url = request.getUrl().toString();if(isValidEmail(url)){ return true;} else if(url.contains(“www.instagram.com“))return true;else if(url.contains(“https://t.me“))返回true;return super.shouldOverrideUrlLoading(view,request);}

@Override
        public void onLoadResource(WebView view, String url) {
            // do nothing
            //super.onLoadResource(view, url);
        }
        public boolean isValidEmail(CharSequence target) {     return (!TextUtils.isEmpty(target) && Patterns.EMAIL_ADDRESS.matcher(target).matches());
        }
    });
7ajki6be

7ajki6be4#

public void onDestroyCode(){

@Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            String url = request.getUrl().toString();
              if (url.contains("mailto:"))
                return true;
             if (url.contains("www.instagram.com"))
                return true;
             if (url.contains("https://t.me"))
                return true;
            else
                return super.shouldOverrideUrlLoading(view, request);
        }
    });

相关问题