Tôi muốn tạo liên kết cho một văn bản textview như Google . Có anyway để thực hiện liên kết như thế này. (tức là) Khi nhấp vào từ Google, nó sẽ mở ra liên kết thích hợp. Mọi ý tưởng đều được hoan nghênh.
Tôi muốn tạo liên kết cho một văn bản textview như Google . Có anyway để thực hiện liên kết như thế này. (tức là) Khi nhấp vào từ Google, nó sẽ mở ra liên kết thích hợp. Mọi ý tưởng đều được hoan nghênh.
Câu trả lời:
Hãy thử điều này và cho tôi biết điều gì sẽ xảy ra ..
Sử dụng mã java:
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));
Từ cấp API> = 24 trở đi Html.fromHtml(String source)
không được dùng nữa thay vào đó hãy sử dụng fromHtml(String, int)
,
textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));
Hoặc trong tệp xml bố cục, bên trong các thuộc tính tiện ích con TextView của bạn
android:autoLink="web"
android:linksClickable="true"
android:autoLink="web"
khỏi XML. Tôi đã có cả hai và nó không hoạt động.
sử dụng android:autoLink="web"
trong xml của TextView của bạn. Nó sẽ tự động chuyển đổi các url có thể nhấp (nếu được tìm thấy trong văn bản)
Tất cả đã được thử nghiệm và hoạt động 100%
Giải pháp: android:autoLink="web"
dưới đây là một ví dụ hoàn chỉnh về
Bố cục Mẫu 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" />
Chuỗi trong 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>
Điều này cũng có thể được thực hiện bằng cách sử dụng thuộc tính mặc định của Textview
android:autoLink="email"
Lưu ý: - Html.fromHtml không được dùng trong Android N
Bạn cần kiểm tra và hỗ trợ Android N
và các phiên bản cao hơn của 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>"));
}
Ngoài ra
Bạn có thể không muốn lập trình thêm cờ Tự động liên kết trên TextView.
android: autoLink = "web"
android: linksClickable = "true"
Bằng cách này, bạn không cần thêm <a href='somelink'>
thẻ.
Đó là một bất lợi, nếu bạn muốn thêm hyperlink
vào text
bạn không thể làm theo cách này. ví dụ bạn không thể làm điều gì đó như thế này: - [ 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" />
Kết quả từ cả hai cách tiếp cận: -
Đối với phiên bản SDK mới nhất fromHtml
không được dùng nữa, Sử dụng dòng dưới đây
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));
}