如何在应用程序的webview活动中打开任何Web链接(没有任何URL方案),而不管默认浏览器

时间:2016-05-26 09:46:46

标签: android textview android-webview

我已经看到了一些关于我的问题的好答案,但没有一个解决方案100%正确。大多数解决方案都说使用自定义方案设置超链接并在IntentFilter中设置相同的方案。

但我正在开发一个消息应用程序,用户可以发送任何随机文本,如google.com,yahoo.com等。现在我在xml中设置属性autoLink = true,这样所有google.com都成为超链接,当用户点击时它在网络浏览器中打开。我希望更改此行为,以便当用户单击任何链接时,它应该打开一个活动所属的同一个应用程序的新webview。

solution1

solution2

3 个答案:

答案 0 :(得分:0)

使用以下代码

public class MainActivity extends Activity {
    private ProgressBar progressBar;
    private WebView webView;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        progressBar.setMax(100);

        webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setAllowContentAccess(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        WebSettings settings = webView.getSettings();
        settings.setPluginState(WebSettings.PluginState.ON);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);
        settings.setDisplayZoomControls(false);

        webView.setWebViewClient(new WebViewClientDemo());
        webView.setWebChromeClient(new WebChromeClientDemo());
        webView.loadUrl("http://business-new.zakoopi.com/#/login/signin");

    }

    private class WebViewClientDemo extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            progressBar.setVisibility(View.GONE);
            progressBar.setProgress(100);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressBar.setVisibility(View.VISIBLE);
            progressBar.setProgress(0);
        }
    }

    private class WebChromeClientDemo extends WebChromeClient {
        public void onProgressChanged(WebView view, int progress) {
            progressBar.setProgress(progress);
        }


        @Override
        public boolean onJsAlert(WebView view, String url, String message,JsResult result) {
            //Required functionality here
            return super.onJsAlert(view, url, message, result);
        }

    }


}

答案 1 :(得分:0)

您可能需要在此处使用 Linkify

String text = "These are some sample links:some random links";
Spannable spannable = new SpannableString( Html.fromHtml(text) );
Linkify.addLinks(spannable, Linkify.WEB_URLS);

现在你必须听取点击次数。你可以这样做。

URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
for (URLSpan urlSpan : spans) {
    LinkSpan linkSpan = new LinkSpan(urlSpan.getURL());
    int spanStart = spannable.getSpanStart(urlSpan);
    int spanEnd = spannable.getSpanEnd(urlSpan);
    spannable.setSpan(linkSpan, spanStart, spanEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannable.removeSpan(urlSpan);
}

同时创建LinkSpan类

 private class LinkSpan extends URLSpan {
        private LinkSpan(String url) {
            super(url);
        }

        @Override
        public void onClick(View view) {
            String url = getURL();
            if (url != null) {

            startActivity(new Intent(LinkTestActivity.this,YourWebactivity.class).putExtra("url",url));

        }
    }
}

答案 2 :(得分:0)

这个解决方案实际上对我有用。但Onclick事件触发了两次,但我正在处理它。

private boolean isClickingLink = false;

TextView textView =(TextView)findViewById(R.id.main_text);     textView.setMovementMethod(LinkMovementMethod.getInstance());

CharSequence charSequence = textView.getText();
SpannableStringBuilder sp = new SpannableStringBuilder(charSequence);

URLSpan[] spans = sp.getSpans(0, charSequence.length(), URLSpan.class);

for (URLSpan urlSpan : spans) {
    CustomSpan customSpan= new CustomSpan (urlSpan.getURL());
    sp.setSpan(customSpan, sp.getSpanStart(urlSpan),
            sp.getSpanEnd(urlSpan), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

textView.setText(sp);

textView.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // 2.if clicking a link
        if (!isClickingLink) {
            Log.w("log", "not clicking link");
        }
        isClickingLink = false;
    }
});

私有类CustomSpan扩展了ClickableSpan {

private String url;

public CustomSpan (String url) {

    super();
    thid.url= url;
}

@Override
public void onClick(View widget) {

    isClickingLink = true;
}

}