티스토리 뷰

반응형

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

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

 

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

Android TextView with Clickable Links: how to capture clicks?

클릭 가능한 링크가 포함된 Android TextView: 클릭을 캡처하는 방법은 무엇입니까?

 문제 내용 

I have a TextView which is rendering basic HTML, containing 2+ links. I need to capture clicks on the links and open the links -- in my own internal WebView (not in the default browser.)

2개 이상의 링크가 포함된 기본 HTML을 렌더링하는 TextView가 있습니다. 링크 클릭을 캡처하고 링크를 열어야 합니다. 이 링크는 기본 브라우저가 아닌 내부 웹뷰에서 열 수 있습니다.

 

The most common method to handle link rendering seems to be like this:

링크 렌더링을 처리하는 가장 일반적인 방법은 다음과 같습니다.
String str_links = "<a href='http://google.com'>Google</a><br /><a href='http://facebook.com'>Facebook</a>";
text_view.setLinksClickable(true);
text_view.setMovementMethod(LinkMovementMethod.getInstance());
text_view.setText( Html.fromHtml( str_links ) );

 

However, this causes the links to open in the default internal web browser (showing the "Complete Action Using..." dialog).

그러나 이렇게 하면 기본 내부 웹 브라우저에서 링크가 열립니다.

 

I tried implementing a onClickListener, which properly gets triggered when the link is clicked, but I don't know how to determine WHICH link was clicked...

링크를 클릭하면 제대로 트리거되는 onClickListener를 구현하려고 했지만 클릭한 링크를 확인하는 방법을 모르겠습니다...
text_view.setOnClickListener(new OnClickListener(){

    public void onClick(View v) {
        // what now...?
    }

});

 

Alternatively, I tried creating a custom LinkMovementMethod class and implementing onTouchEvent...

또는 사용자 지정 LinkMovementMethod 클래스를 만들고 TouchEvent에서 구현하려고 했습니다...
public boolean onTouchEvent(TextView widget, Spannable text, MotionEvent event) {
    String url = text.toString();
    // this doesn't work because the text is not necessarily a URL, or even a single link... 
    // eg, I don't know how to extract the clicked link from the greater paragraph of text
    return false;
}

 

Ideas?

다른 아이디어가 있을까요?

 


Example solution

예제 솔루션

 

I came up with a solution which parses the links out of a HTML string and makes them clickable, and then lets you respond to the URL.

저는 HTML 문자열에서 링크를 구문 분석하여 클릭 가능하게 만든 다음 URL에 응답할 수 있는 솔루션을 생각해 냈습니다.

 

 

 

 높은 점수를 받은 Solution 

Based upon another answer, here's a function setTextViewHTML() which parses the links out of a HTML string and makes them clickable, and then lets you respond to the URL.

다른 답변을 기반으로 HTML 문자열에서 링크를 구문 분석하고 클릭 가능하게 만든 다음 URL에 응답할 수 있는 setTextViewHTML() 함수가 있습니다.
protected void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span)
{
    int start = strBuilder.getSpanStart(span);
    int end = strBuilder.getSpanEnd(span);
    int flags = strBuilder.getSpanFlags(span);
    ClickableSpan clickable = new ClickableSpan() {
        public void onClick(View view) {
            // Do something with span.getURL() to handle the link click...
        }
    };
    strBuilder.setSpan(clickable, start, end, flags);
    strBuilder.removeSpan(span);
}

protected void setTextViewHTML(TextView text, String html)
{
    CharSequence sequence = Html.fromHtml(html);
    SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
    URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);   
    for(URLSpan span : urls) {
        makeLinkClickable(strBuilder, span);
    }
    text.setText(strBuilder);
    text.setMovementMethod(LinkMovementMethod.getInstance());       
}

 

 

 가장 최근 달린 Solution 

I made an easy extension function in Kotlin to catch url link clicks in a TextView by applying a new callback to URLSpan elements.

저는 URLspan 요소에 새로운 콜백을 적용하여 TextView에서 URL 링크 클릭을 잡기 위해 Kotlin에서 쉬운 확장 기능을 만들었습니다.

 

strings.xml (example link in text)

strings.xml(텍스트에 링크 표시)
<string name="link_string">this is my link: <a href="https://www.google.com/">CLICK</a></string>

 

Make sure your spanned text is set to the TextView before you call "handleUrlClicks"

"handleUrlClicks"를 호출하기 전에 스판 텍스트가 TextView로 설정되어 있는지 확인하십시오.
textView.text = getString(R.string.link_string)

 

This is the extension function:

확장 함수는 다음과 같습니다.
/**
 * Searches for all URLSpans in current text replaces them with our own ClickableSpans
 * forwards clicks to provided function.
 */
fun TextView.handleUrlClicks(onClicked: ((String) -> Unit)? = null) {
    //create span builder and replaces current text with it
    text = SpannableStringBuilder.valueOf(text).apply {
        //search for all URL spans and replace all spans with our own clickable spans
        getSpans(0, length, URLSpan::class.java).forEach {
            //add new clickable span at the same position
            setSpan(
                object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        onClicked?.invoke(it.url)
                    }
                },
                getSpanStart(it),
                getSpanEnd(it),
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE
            )
            //remove old URLSpan
            removeSpan(it)
        }
    }
    //make sure movement method is set
    movementMethod = LinkMovementMethod.getInstance()
}

 

This is how I call it:

그것을 호출하는 코드는 아래와 같습니다.
textView.handleUrlClicks { url ->
    Timber.d("click on found span: $url")
}

 

 

출처 : https://stackoverflow.com/questions/12418279/android-textview-with-clickable-links-how-to-capture-clicks

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