티스토리 뷰

반응형

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

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

 

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

How to set the part of the text view is clickable

텍스트 뷰에서 일부 텍스트를 클릭 가능하게 설정하는 방법

 문제 내용 

I have the text "Android is a Software stack". In this text i want to set the "stack" text as clickable. So, if you click on that it will redirected to a new activity(not in the browser).

"Android is a Software stack"이라는 텍스트가 있습니다. 이 텍스트에서 "stack" 텍스트를 클릭 가능하게 설정하고 해당 텍스트를 클릭하면 새로운 액티비티(브라우저가 아님)로 이동하도록 하려고 합니다.

 

I tried but i am not getting a solution.

해결책을 찾으려고 했지만 해결하지 못했습니다.

 

 

 

 높은 점수를 받은 Solution 

android.text.style.ClickableSpan can solve your problem.

android.text.style.ClickableSpan를 사용하면 문제를 해결할 수 있습니다.
SpannableString ss = new SpannableString("Android is a Software stack");
ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(View textView) {
        startActivity(new Intent(MyActivity.this, NextActivity.class));
    }
    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setUnderlineText(false);
    }
};
ss.setSpan(clickableSpan, 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

TextView textView = (TextView) findViewById(R.id.hello);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);

 

In XML:

<TextView 
  ...
  android:textColorLink="@drawable/your_selector"
/>

 

 

 가장 최근 달린 Solution 

For those that are looking for a solution in Kotlin here is what worked for me:

Kotlin에서 해결책을 찾고 있는 사람들을 위해 다음이 제대로 작동하는 방법입니다.
private fun setupTermsAndConditions() {
    val termsAndConditions = resources.getString(R.string.terms_and_conditions)
    val spannableString = SpannableString(termsAndConditions)
    val clickableSpan = object : ClickableSpan() {
        override fun onClick(widget: View) {
            if (checkForWifiAndMobileInternet()) {
                // binding.viewModel!!.openTermsAndConditions()
                showToast("Good, open the link!!!")

            } else {
                showToast("Cannot open this file because of internet connection!")
            }

        }

        override fun updateDrawState(textPaint : TextPaint) {
            super.updateDrawState(textPaint)
            textPaint.color = resources.getColor(R.color.colorGrey)
            textPaint.isFakeBoldText = true
        }
    }

    spannableString.setSpan(clickableSpan, 34, 86, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
    binding.tvTermsAndConditions.text = spannableString
    binding.tvTermsAndConditions.movementMethod = LinkMovementMethod.getInstance()
    binding.tvTermsAndConditions.setHighlightColor(Color.TRANSPARENT);

}

 

 

출처 : https://stackoverflow.com/questions/10696986/how-to-set-the-part-of-the-text-view-is-clickable

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