[android] Android에서 하이퍼 링크 텍스트보기 만들기

Google 과 같은 textview 텍스트에 대한 링크를 만들고 싶습니다 . 어쨌든 이와 같은 링크를 만들 수 있습니까? (ie) Google이라는 단어를 클릭하면 적절한 링크가 열립니다. 모든 아이디어를 환영합니다.



답변

이것을 시도하고 무슨 일이 일어나는지 알려주십시오 ..

자바 코드 사용 :

TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));

API 레벨> = 24부터는 Html.fromHtml(String source)더 이상 사용되지 않습니다. 대신 fromHtml(String, int),

textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));

또는 레이아웃 xml 파일에서 TextView 위젯 속성 내부

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


답변

android:autoLink="web"TextView의 xml에서 사용 하십시오. 클릭 가능한 URL을 자동으로 변환해야합니다 (텍스트에서 찾은 경우).


답변

모든 테스트 및 작동 100 %
솔루션 : android:autoLink="web"
아래는 전체 예제

샘플 레이아웃 Xml입니다.

    <TextView
        android:id="@+id/txtLostpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:autoLink="email"
        android:gravity="center"
        android:padding="20px"
        android:text="@string/lostpassword"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/txtLostpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:autoLink="web"
        android:gravity="center"
        android:padding="20px"
        android:text="@string/defaultpassword"
        android:textAppearance="?android:attr/textAppearanceSmall" />

string.xml의 문자열

<string name="lostpassword">If you lost your password please contact <a href="mailto:support@cleverfinger.com.au?Subject=Lost%20Password" target="_top">support@cleverfinger.com.au</a></string>

<string name="defaultpassword">User Guide <a href="http://www.cleverfinger.com.au/user-guide/">http://www.cleverfinger.com.au/user-guide/</a></string>


답변

Textview 의 기본 속성을 사용하여 수행 할 수도 있습니다.

android:autoLink="email"


답변

참고 :-Html.fromHtml은 Android N에서 더 이상 사용되지 않습니다.

당신 Android N은 안드로이드의 더 높은 버전을 확인하고 지원해야합니다

                  //Set clickable true
                 tagHeading.setClickable(true);

                  //Handlle click event
                  tagHeading.setMovementMethod(LinkMovementMethod.getInstance());

                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                    tagHeading.setText(Html.fromHtml("<a href='https://github.com/hiteshsahu'>https://github.com/hiteshsahu</a>", Html.FROM_HTML_MODE_LEGACY));
                } else {
                    tagHeading.setText(Html.fromHtml("<a href='https://github.com/hiteshsahu'>https://github.com/hiteshsahu</a>"));
                }

또는

프로그래밍 방식으로 TextView에 autoLink 플래그를 추가하는 것을 원하지 않을 수 있습니다.

android : autoLink = “web”

android : linksClickable = “true”

이렇게하면 <a href='somelink'>태그 를 추가 할 필요가 없습니다 .

어떤 추가 할 경우, 단점입니다 hyperlinktext이 방법으로 그것을 할 수 없습니다. 예를 들어 다음과 같이 할 수 없습니다 :-[ hiteshsahu ] [1]

           <TextView
                android:id="@+id/tag_info"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/tag_ll"
                android:layout_gravity="left"
                android:layout_margin="10dp"
                android:autoLink="web"
                android:linksClickable="true"
                android:text="https://github.com/hiteshsahu"
                android:textColor="@color/secondary_text" />

두 접근법의 결과 :-

https://github.com/hiteshsahu


답변

최신 버전의 SDK fromHtml는 더 이상 사용되지 않습니다. 아래 줄을 사용하십시오.

String yourtext = "<a style='text-decoration:underline' href='http://www.sample.com'> Sample Website </a>";
    if (Build.VERSION.SDK_INT >= 24) {
        textView.setText(Html.fromHtml(yourtext, Html.FROM_HTML_MODE_LEGACY));
    } else {
        textView.setText(Html.fromHtml(yourtext));
    }


답변

textview의 다음 확장 기능을 만들었습니다.

@SuppressLint("SetTextI18n")
fun TextView.makeHyperLink(url: String) {
    text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Html.fromHtml("<a href='${url}'>${text}</a>", Html.FROM_HTML_MODE_COMPACT)
    } else {
        Html.fromHtml("<a href='${url}'>${text}</a>")
    }
    movementMethod = LinkMovementMethod.getInstance()
    isClickable = true
}

이렇게 부르고

tvTos.makeHyperLink(tos_url)

텍스트를 매개 변수로 전달할 수도 있습니다.