Zrobiłem to samo w jednej z moich aplikacji. Proszę postępować zgodnie z poniższym kodem.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/margin"
tools:context="tdsolutions.com.clickabletext.MainActivity">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World this text" />
</LinearLayout>
Przełóż TextView poniżej metody (setClickableText):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = (TextView) findViewById(R.id.text);
setClickableText(text);
}
private void setClickableText(TextView textView) {
String text = "this"; // the text you want to mark as a link
SpannableString ss = new SpannableString(textView.getText());// full text
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.parseColor("#91ca46"));//link text color
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
//What ever the code you want when click on text goes here
Toast.makeText(MainActivity.this,"you clicked",Toast.LENGTH_LONG).show();
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
};
int start = textView.getText().toString().indexOf(text);
int length = start + text.length();
ss.setSpan(clickableSpan, start, length, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ss.setSpan(fcs, start, length, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);
}
Lub możesz skorzystać z poniższego kodu:
dodać widok tekstowy, jak poniżej, zmiany bez względu na atrybuty, jak chcesz.
<TextView
android:id="@+id/textView"
android:layout_centerInParent="true"
android:linksClickable="true"
android:textColorLink="#00f"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
następnie dodać poniżej kodu:
TextView textView = (TextView) findViewById(R.id.textView);
Spanned result;
String html = "Hi sweetheart go to <a href=\"http://www.google.com\">this</a> link";
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
result = Html.fromHtml(html ,Html.FROM_HTML_MODE_LEGACY);
} else {
result = Html.fromHtml(html);
}
textView.setText(result);
textView. setMovementMethod(LinkMovementMethod.getInstance());
upewnij się, gdy dodasz linki zastąpić je znaczniki HTML jak ja na przykład String html = "Hi sweetheart go to <a href=\"http://www.google.com\">this</a> link";
Wygląda na to, że są po prostu tworzenie HTML z link w znaczniku zakotwiczenia. – riggaroo
To wygląda tak samo. Ale nie mam pojęcia, jak to zrobić w SMS-ie z Deep Link. Chciałem zamiast tego wyświetlić tekst z hiperłączami, jeśli jest pełny adres URL. Jakieś sugestie ? – CoDe
Nie sądzę, że jest to SMS w tym przykładzie, o ile wiem, że SMS-y nie są sformatowane przez HTML, więc nie byłoby to możliwe. – riggaroo