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"
Note :- Html.fromHtml is deprecated in Android N
You need to do check and support Android N
and higher versions of Android
//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>"));
}
Alternatively
You can don't want to id programmatically add autoLink flag on TextView.
android:autoLink="web"
android:linksClickable="true"
This way You don't need to add <a href='somelink'>
tags.
Which is a disadvantage, if you want to add hyperlink
on a text
you can't do it this way. eg you can't do something like this:- [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" />
The result from both approach:-
For Latest version of SDK fromHtml
is deprecated Use below line
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));
}
참고URL : https://stackoverflow.com/questions/9290651/make-a-hyperlink-textview-in-android
'IT Share you' 카테고리의 다른 글
System.Windows.Interactivity를 프로젝트에 추가하는 방법은 무엇입니까? (0) | 2020.11.12 |
---|---|
자동 증분 기본 키를 사용하여 MySQL에 데이터를 삽입하는 방법은 무엇입니까? (0) | 2020.11.12 |
jQuery DataTables를 사용할 때 첫 번째 열에서 자동 정렬 비활성화 (0) | 2020.11.12 |
MySQL Workbench가 쿼리 결과를 표시하지 않음 (0) | 2020.11.12 |
8.0 이전 iOS 버전의 레이아웃 여백과 관련된 레이아웃 속성 (0) | 2020.11.12 |