2011-07-09 36 views
7

链接我有这样的一段代码:打开一个网页视图,而不是默认的浏览器

TextView noteView = (TextView) view.findViewById(R.id.content); 
    noteView.setMovementMethod(LinkMovementMethod.getInstance()); 
    noteView.setText(Html.fromHtml(noteView.getText().toString())); 

我需要在网页视图中打开链接,而不是浏览器...这可能吗?我能怎么做??

在此先感谢..

回答

5

是的,你可以做到这一点,这是很简单的任务与WebView中,你需要声明一个WebViewClient对象和覆盖public boolean shouldOverrideUrlLoading (WebView view, String url)方法,您可以过滤网址或给予一定的定制功能。

在你的情况下,要留在WebView上,你需要在该方法上返回false。

结账this tutorial

问候

编辑:

看来你的问题是如何处理上的TextView的网址点击事件。正如it's suggested on this question,你可以过滤包含Activity的WebView中的ACTION_VIEW意图。如果您需要more guidance about intent-filters, check this out

+0

谢谢,但是,这不起作用...我在开始时没有WebView ...当我点击一个链接时,我将显示网络视图而不是浏览器^^是吗?不可能性? – Erenwoid

+0

@Erenwoid,现在我得到你的问题,你想在TextView url的点击时处理Intent。那么看看我编辑的答案。 – mdelolmo

+0

那岩石^^谢谢! – Erenwoid

7

另外,你可以这样做。

package com.TextHtml; 

    import android.app.Activity; 
    import android.content.Context; 
    import android.os.Bundle; 
    import android.text.Html; 
    import android.text.Spannable; 
    import android.text.SpannableStringBuilder; 
    import android.text.method.LinkMovementMethod; 
    import android.text.style.ClickableSpan; 
    import android.text.style.URLSpan; 
    import android.view.View; 
    import android.widget.TextView; 
    import android.widget.Toast; 
    public class TextHtml extends Activity { 

     private TextView tv; 
     static Context ctx=null; 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      ctx=this; 
      tv = (TextView) findViewById(R.id.tv); 
      String htmlLinkText = "<a href="/" mce_href="/""http://www.google.com/"><u>hello google </u></a>";  
      tv.setText(Html.fromHtml(htmlLinkText)); 
      tv.setMovementMethod(LinkMovementMethod.getInstance());  
      CharSequence text = tv.getText();  
      if(text instanceof Spannable){  
       int end = text.length();  
       Spannable sp = (Spannable)tv.getText();  
       URLSpan[] urls=sp.getSpans(0, end, URLSpan.class);  
       SpannableStringBuilder style=new SpannableStringBuilder(text);  
       style.clearSpans();//should clear old spans  
       for(URLSpan url : urls){  
        CustomerTextClick click = new CustomerTextClick(url.getURL());  
        style.setSpan(click,sp.getSpanStart(url),sp.getSpanEnd(url),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);  
       }  
       tv.setText(style);  
      } 
     } 

     private static class CustomerTextClick extends ClickableSpan{  

      private String mUrl;  
      MyURLSpan(String url) {  
       mUrl =url;  
      }  
      @Override 
      public void onClick(View widget) { 
       // TODO Auto-generated method stub 
       Toast.makeText(ctx, "hello google!",Toast.LENGTH_LONG).show(); 
      }  
     } 
    } 
+0

如果您不想将意图过滤器添加到Web视图活动,最佳答案。 –

+0

有史以来最佳答案! – Michalsx

相关问题