Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

android - Open a link in a webview instead of default browser

I have this piece of code:

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

I need to open links in a webview, not in a browser... is this possible?? how can I do??

Thanks in advance..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

additional, you can do this.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...