티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

How to make links in a TextView clickable

텍스트뷰 링크를 클릭할 수 있도록 하는 방법

 문제 내용 

I have the following TextView defined:

다음과 같은 TextView가 정의되어 있습니다.
<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="@string/txtCredits"
    android:autoLink="web" android:id="@+id/infoTxtCredits"
    android:layout_centerInParent="true"
    android:linksClickable="true"/>

 

where @string/txtCredits is a string resource that contains <a href="some site">Link text</a>.

여기서 @string/txtCredits는 링크 텍스트를 포함하고 있습니다.

 

Android is highlighting the links in the TextView, but they do not respond to clicks. What am I doing wrong? Do I have to set an onClickListener for the TextView in my activity for something as simple as this?

Android가 텍스트뷰에서 링크를 강조 표시하고 있지만 클릭에 응답하지 않습니다. 제가 뭘 잘못하고 있는 것인가요? 이렇게 간단한 작업을 위해 액티비티에서 텍스트뷰에 대해 클릭 리스너를 설정해야 합니까?

 

It looks like it has to do with the way I define my string resource.

문자열 리소스를 정의하는 방법과 관련이 있는 것 같습니다.

 

This does not work:

이것은 작동하지 않습니다.

 

<string name="txtCredits"><a href="http://www.google.com">Google</a></string>

 

But this does:

하지만 이것은 다음과 같습니다:
<string name="txtCredits">www.google.com</string>

 

Which is a bummer because I would much rather show a text link than show the full URL.

전체 URL을 표시하는 것보다 텍스트 링크를 표시하는 것이 더 낫기 때문에 안타까운 일입니다.

 

 

 

 높은 점수를 받은 Solution 

Buried in the API demos, I found the solution to my problem:

API 데모에 파묻혀 문제 해결책을 찾았습니다.

 

File Link.java:

    // text2 has links specified by putting <a> tags in the string
    // resource.  By default these links will appear but not
    // respond to user input.  To make them active, you need to
    // call setMovementMethod() on the TextView object.

    TextView t2 = (TextView) findViewById(R.id.text2);
    t2.setMovementMethod(LinkMovementMethod.getInstance());

 

I removed most of the attributes on my TextView to match what was in the demo.

데모에 있는 것과 일치하도록 텍스트뷰의 속성을 대부분 제거했습니다.
<TextView
    android:id="@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/txtCredits"/>

 

That solved it. It is pretty difficult to uncover and fix.

그걸로 해결됐어요. 발견하고 고치는 것은 꽤 어렵습니다.

 

Important: Don't forget to remove autoLink="web" if you are calling setMovementMethod().

중요: setMovementMethod()를 호출하는 경우에는 autoLink="web"을 제거하는 것을 잊지 마십시오.

 

 

 

 가장 최근 달린 Solution 

Add this to your EditText:

텍스트 편집에 추가:

 

android:autoLink="web"
android:linksClickable="true"

 

 

출처 : https://stackoverflow.com/questions/2734270/how-to-make-links-in-a-textview-clickable

반응형
댓글
공지사항
최근에 올라온 글