user6457941 user6457941 - 4 months ago 49
Android Question

how to control textView onclicklistener with autolink web setting or in other words,intercept autolink web OnClick event?

How to control textView

onclicklistener
with
autolink
web setting or in other words,intercept
autolink web OnClick
event?

For example,
String text="Lucy is very nice.Here is her link.https://www.google.com";textview.setText(text);

when clicking "https://www.google.com",I can catch it and jump to my app activity not to web browser.
Textview has a property “autolink”.I set autolink as web.android:autoLink="web" So,android system can automatically detect the url.When clicking the url,it will jump to the browser.Now when clicking, I do not want jump to the brower,I just want to jump to my app activity and stay in app.

Answer

Thanks you for all of your answers.Now I find my question's answer.There are two steps.

1.you need set Textview property. android:autoLink="web"

  <TextView  
    android:id="@+id/text_view"  
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:autoLink="web"  
    android:text="Lucy is very nice.Here is her link.https://www.google.com" />

2.override URL onclick.There is an example.

public class MainActivity extends Activity {

TextView tv;  

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  
    tv = (TextView) findViewById(R.id.tv);  
    CharSequence text = tv.getText();  
    if (text instanceof Spannable) {  
        int end = text.length();  
        Spannable sp = (Spannable) text;  
        URLSpan urls[] = sp.getSpans(0, end, URLSpan.class);  
        SpannableStringBuilder style = new SpannableStringBuilder(text);  
        style.clearSpans();  
        for (URLSpan urlSpan : urls) {  
            MyURLSpan myURLSpan = new MyURLSpan(urlSpan.getURL());  
            style.setSpan(myURLSpan, sp.getSpanStart(urlSpan),  
                    sp.getSpanEnd(urlSpan),  
                    Spannable.SPAN_EXCLUSIVE_INCLUSIVE);  

        }  
        tv.setText(style);  
    }  
}  

private class MyURLSpan extends ClickableSpan {  

    private String url;  

    public MyURLSpan(String url) {  
        this.url = url;  
    }  

    @Override  
    public void onClick(View arg0) {  
        Toast.makeText(MainActivity.this, url, Toast.LENGTH_LONG).show(); 
    }  

}  

}

3.The above code perfectly solved my problem.So when I click www.google.com in the Textview,the url will show out and jump to a specific activity.

Comments