Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
964 views
in Technique[技术] by (71.8m points)

android - Disable the automatic Linkify of Webview

I have a webview that I am creating. It seems to automatically be linkifying numbers into tel: urls. I didn't see a way to remove this ability (at least nothing similar to the way to enable it on a textview).

The code is pretty simple:

// populate the web view
WebView webView = (WebView) findViewById(R.id.app_info_webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_INSIDE_OVERLAY);

webView.setBackgroundColor(0);
String url = APP_INFO_BODY_HTML + "?versionName=" + versionName;

webView.loadUrl(url);

I have a copyright notice at the bottom of the page, android is changing the 2011 into a clickable link that opens the dialer. Also, the App version 1.0.0 opens in the dialer.

Is there a way to disable this functionality?

Update: I just discovered that this seems device dependent...happens on the Droid X, but not a Samsung Captivate, not on Nexus S, and not the emulator.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is a way to do that - rather ugly, two layered, but still a workaround.

You should

  1. modify how the webview will handle the auto-linkifiable items
  2. explicitly tell the loaded page not to apply styles and haptic feedback.

    mWebView.setWebViewClient( new WebViewClient() {
    
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, final String url) {
        Uri uri = Uri.parse(url);
    
        //TODO analyse the uri here 
        //and exclude phone and email from triggering any action
    
        return false;
    }
    
    public void onReceivedError(WebView view, int errorCode, 
                                            String description, String failingUrl) {}
    
    public void onPageFinished (WebView view, String url) {...}
    
    public void onPageStarted(WebView view, String url, Bitmap favicon) {...}
    
    public void onLoadResource(WebView view, String url) {...}
    }); 
    

    In the html specify the following meta tags inside the tag:

    <meta name="format-detection" content="telephone=no" />
    <meta name="format-detection" content="email=no" />
    

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...