TextView中有多个URL



在我的应用程序中,我有一个包含multiple URLstextview。只有当我使用时,我才能在我的应用程序中打开它

android:autoLink="web"
android:clickable="true"

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

创建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视图活动类中使用以下代码行

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);
        }
    }
}

使用以下代码从您想要的位置调用web视图活动类

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);

请检查以下代码。

我试过使用下面的代码。

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类

public class InternalURLSpan extends ClickableSpan {
    OnClickListener mListener;
    public InternalURLSpan(OnClickListener listener) {
        mListener = listener;
    }
    @Override
    public void onClick(View widget) {
        mListener.onClick(widget);
    }
}

相关内容

  • 没有找到相关文章

最新更新