TextView 中的多个 URL

Multiple URLs in TextView

在我的应用程序中,我有一个包含 multiple URLstextview。仅当我使用

时,我才必须在我的应用程序中打开它
android:autoLink="web"
android:clickable="true"

这里它在 browser 中打开,但我必须在我的 Webview 中打开。

请检查以下代码。

我试过使用下面的代码。

TextView tv = ....
tv.setMovementMethod(LinkMovementMethod.getInstance());

    String content = tv.getText().toString();
    List<String> links = new ArrayList<String>();

    Pattern p = Patterns.WEB_URL;
    Matcher m = p.matcher(content);
    while (m.find()) {
        String urlStr = m.group();
        links.add(urlStr);
    }

    SpannableString f = new SpannableString(content);

    for (int i = 0; i < links.size(); i++) {
        final String url = links.get(i);

        f.setSpan(new InternalURLSpan(new OnClickListener() {
            public void onClick(View v) {
                Context ctx = v.getContext();
                String urlToOpen = url;
                if (!urlToOpen.startsWith("http://") || !urlToOpen.startsWith("https://"))
                    urlToOpen = "http://" + urlToOpen;
                openURLInBrowser(urlToOpen, ctx);
            }
        }), content.indexOf(url), content.indexOf(url) + url.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    tv.setText(f);

希望对你有所帮助

InternalURLSpan class

public class InternalURLSpan extends ClickableSpan {
    OnClickListener mListener;

    public InternalURLSpan(OnClickListener listener) {
        mListener = listener;
    }

    @Override
    public void onClick(View widget) {
        mListener.onClick(widget);
    }
}

创建 Web 视图布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/web_view_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_image"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/image1" />

</RelativeLayout>

在您的 Web 视图中使用以下代码行 activity class

public class WebviewActivity extends Activity
{
    private WebView         m_WebView;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview_page);

        Bundle extras = getIntent().getExtras();
        if(extras != null)
        {
            m_SelectedLink = extras.getString("SelectedLink");
        }

        if(m_SelectedLink != null)
        {
            m_WebView = (WebView) findViewById(R.id.webView1);
            m_WebView.setInitialScale(1);
            m_WebView.getSettings().setBuiltInZoomControls(true);
            m_WebView.getSettings().setLoadWithOverviewMode(true);
            m_WebView.getSettings().setUseWideViewPort(true);
            m_WebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
            m_WebView.setScrollbarFadingEnabled(false);         
            m_WebView.setWebViewClient(new WebViewController());
            m_WebView.loadUrl(m_SelectedLink);
        }                
    }   

    public class WebViewController extends WebViewClient 
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) 
        {          
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) 
        {
            view.loadUrl(url);
            return true;
        }    

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

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
        {
             progressBar.setVisibility(View.GONE);
             super.onReceivedError(view, errorCode, description, failingUrl);
        }
    }
}

使用下面的代码

从你想要的任何地方调用网络视图activityclass
Bundle extras = new Bundle();
extras.putString("SelectedLink", "http://plackal.in/terms-and-privacy-policy/");
Intent intent = new Intent(this, WebviewActivity.class);
intent.putExtras(extras);
startActivity(intent);