android - 在 webview 而不是默认浏览器中打开链接

标签 android webview

我有这段代码:

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

我需要在 WebView 中打开链接,而不是在浏览器中...这可能吗??我该怎么办??

提前致谢..

最佳答案

另外,你可以这样做。

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

关于android - 在 webview 而不是默认浏览器中打开链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6636283/

相关文章:

java - 在两个相对文本字段上使用 TextChanged 监听器

android - 可以在Worker中使用WebView吗?

jquery - 移动第一 : adapter returned data containing special characters

android - 如何阻止 webview 中的任何广告,例如浏览器中的 adblock 或 adblock plus?

android - InflateException 扩展 android.preference.ListPreference

android - 如何使用 TarsosDSP 获取 MFCC?

javascript - Android webview 使用 GSAP : Attempt to remove non-JNI local reference, 转储线程

android - "html/text"Android web View 中的图像?没有任何 html 文件

android - 如何在自定义适配器中初始化 ProgressDialog

android - 使用 Google 应用内结算的运营商结算授权失败