Android active link of url in TextView

前端 未结 10 489
盖世英雄少女心
盖世英雄少女心 2020-11-30 22:24

I have getting dynamic text from a web service and showing the same in a TextView. Sometimes the TextView has url like hello

相关标签:
10条回答
  • 2020-11-30 22:43

    There are 2 cases:

    • the text looks like "click on http://www.hello.com"

    then you just have to set the autoLink attribute in the xml so that the link is automatically detected in the text:

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autoLink="web"
        android:text="click on http://www.hello.com"/>
    
    • the text looks like click on <a href="http://hello.com">hello</a>

    then you have to do it by code and tell the text is html, and specify a Link movement method for the click:

        String text = "click on <a href=\"http://hello.com\">hello</a>";
        TextView textView = view.findViewById(R.id.textView);
        textView.setText(Html.fromHtml(text));
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    
    0 讨论(0)
  • 2020-11-30 22:44

    This works for me:

    <TextView
        android:text="www.hello.com"
        android:id="@+id/TextView01"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent"
        android:autoLink="web">
    </TextView>
    
    0 讨论(0)
  • 2020-11-30 22:44

    i have give some idea which i have found it

    TextView tv = ( TextView ) findViewById( R.id.link );  
        WebView wv = ( WebView ) findViewById( R.id.webView );  
        URLSpan[] urlSpans = tv.getUrls();  
        for ( URLSpan urlSpan : urlSpans )  
        {  
            wv.loadUrl( urlSpan.getURL() );  
        }  
    

    string.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <resources>  
      <string name="app_name">Hello, Android</string>  
      <string name="link">'<a href="http://www.google.com" rel="nofollow">Google</a>'</string>  
    </resources> 
    

    main.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout  
            xmlns:android="http://schemas.android.com/apk/res/android"  
            android:orientation="vertical"  
            android:layout_width="fill_parent"  
            android:layout_height="fill_parent"  
            >  
    
      <TextView  
              android:id="@+id/link"  
              android:layout_width="wrap_content"  
              android:layout_height="wrap_content"  
              android:autoLink="all"  
              android:linksClickable="true"  
              android:text="@string/link" />  
    
      <WebView  
              android:id="@+id/webView"  
              android:layout_width="fill_parent"  
              android:layout_height="wrap_content"  
              android:layout_weight="1.0" />  
    
    </LinearLayout>  
    
    0 讨论(0)
  • 2020-11-30 22:45

    In your XML, you need to add android:linksClickable="true" in the TextView.

    0 讨论(0)
  • 2020-11-30 22:48

    To Save any one time the true solution is

        <TextView
        android:text="www.hello.com"
        android:id="@+id/TextView01"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent"
        android:autoLink="web"
        android:linksClickable="true">
    </TextView>
    

    and i use it and it work perfect

    0 讨论(0)
  • 2020-11-30 22:49

    If you are displaying in textview the string from strings.xml, strings containing the web link should not have word "a href=". If these words are deleted from the strings.xml file then the link will work.

    0 讨论(0)
提交回复
热议问题