Nice programing

TextView에서 URL의 Android 활성 링크

nicepro 2020. 12. 31. 23:25
반응형

TextView에서 URL의 Android 활성 링크


웹 서비스에서 동적 텍스트를 가져오고 TextView에 동일하게 표시합니다. 때로는 TextView에 <a href="http://hello.com">hello</a>. 다음 코드를 사용하여 텍스트를 설정했습니다.

textView.setText(Html.fromHtml(sampletext));

또한 android:autoLink="web"해당 xml에 TextView. 이제 링크가 파란색과 밑줄로 제대로 표시되지만 죽은 링크 만 발견되었습니다. 클릭하려고해도 아무 일도 일어나지 않습니다. 링크를 활성화하려면 어떻게해야합니까?


모든 솔루션을 다시 검토 한 후 몇 가지 설명이 포함 된 요약 :

android:autoLink="web" 

android : linksClickable이 설정되지 않은 경우에도 URL을 찾고 링크를 생성하며 링크는 기본적으로 클릭 가능합니다. URL을 단독으로 유지할 필요가 없습니다. 텍스트 중간에서도 감지되고 클릭 가능합니다.

<TextView
    android:text="My web site: www.stackoverflow.com"
    android:id="@+id/TextView1"
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"
    android:autoLink="web">
</TextView>

코드를 통해 링크를 설정하려면 동일한 원칙, 패턴 또는 레이아웃의 android : autoLink가 필요하지 않습니다. Linkify를 사용하여 링크를 자동으로 찾습니다.

  final TextView myClickableUrl = (TextView) findViewById(R.id.myClickableUrlTextView);
  myClickableUrl.setText("Click my web site: www.stackoverflow.com");
  Linkify.addLinks(myClickableUrl, Linkify.WEB_URLS);

이것은 나를 위해 작동합니다.

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

한 번만 저장하려면 진정한 해결책은

    <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>

그리고 나는 그것을 사용하고 완벽하게 작동합니다


두 가지 경우가 있습니다.

  • 텍스트는 다음과 같습니다 "click on http://www.hello.com"

그런 다음 텍스트에서 링크가 자동으로 감지되도록 xml에서 autoLink 속성 을 설정하기 만하면 됩니다.

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:text="click on http://www.hello.com"/>
  • 텍스트는 다음과 같습니다 click on <a href="http://hello.com">hello</a>

그런 다음 코드로 수행하고 텍스트가 html임을 알리고 클릭에 대한 링크 이동 방법을 지정해야합니다.

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

이 접근 방식을 확인하십시오.

String text = "Visit stackoverflow.com";
TextView label = new TextView(this);
label.setText(text);
Pattern pattern = Pattern.compile("stackoverflow.com");
Linkify.addLinks(label, pattern, "http://");

나는 내가 그것을 찾은 아이디어를 주었다

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>  

XML android:linksClickable="true"에서 TextView 에 추가해야합니다 .


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.


Add these lines of code to your textView in xml File it will work perfectly fine..

android:autoLink="web" android:textColorLink="@android:color/holo_orange_dark" android:linksClickable="true"

or if want a your own link in textview add these lines of code in your java file

  final SpannableString s = new SpannableString("https://play.google.com/store/apps/details?id=cn.wps.moffice_eng");
  Linkify.addLinks(s, Linkify.ALL);

set this 's' String in your TextView by function

Textview.setText(s);

and don't forget to add this line

 ((TextView)findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());

id will be your textview id

Enjoy ...


Answer is right, BUT not complete, because I had in my xml some properties from other answers like autoLink and linksClickable and programatic way did not work. Also when I passes string with html from string resource also it did not work, so beware, you have to clean your xml and pass string directly exactly as in that answer.

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

I did not try it without without LinkMovementMethod but now I am ok as it finally work. Other answers did not work for me or was for visible url text, not clickable text as link.

ReferenceURL : https://stackoverflow.com/questions/6910703/android-active-link-of-url-in-textview

반응형