我的文字包含了我希望在文字视图中可点击的文字。
我发现这个好评的答案here
文本元素看起来像textview中的链接,但点击功能不会被触发。
我的XML:
<TextView
android:id="@+id/mainTextView"
android:layout_width="0dp"
android:layout_height="193dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:clickable="true"
android:enabled="false"
android:linksClickable="true"
android:text="@string/main_text"
app:layout_constraintBottom_toTopOf="@+id/linearLayout2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
这里是我的代码:
setTextViewHTML(lv_mainTextView, getResources().getString(R.string.main_text));
...
protected void makeLinkClickable(SpannableStringBuilder strBuilder, final URLSpan span)
{
int start = strBuilder.getSpanStart(span);
int end = strBuilder.getSpanEnd(span);
int flags = strBuilder.getSpanFlags(span);
ClickableSpan clickable = new ClickableSpan() {
@Override
public void onClick(View view) {
// Do something with span.getURL() to handle the link click...
Toast.makeText(getApplicationContext(), "Click", Toast.LENGTH_LONG).show();
}
};
strBuilder.setSpan(clickable, start, end, flags);
strBuilder.removeSpan(span);
}
protected void setTextViewHTML(TextView text, String html)
{
CharSequence sequence = Html.fromHtml(html);
SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
for(URLSpan span : urls) {
makeLinkClickable(strBuilder, span);
}
text.setText(strBuilder);
text.setMovementMethod(LinkMovementMethod.getInstance());
}
当我调试它时,正在调用函数“makeLinkClickable”但是从不触发onCLick。
任何想法和帮助将不胜感激!!!
谢谢Kev
编辑:
我在资源中的文字看起来像这样:
"The <a href="tree">tree</a> is larger than the <a href="flower">flower</a>"
在使用fromHTML获取时,它给了我以下字符串:
"The <a href="tree">tree</a> is larger than the <a href="flower">flower</a>"
答案 0 :(得分:1)
您的代码是正确的。只需删除此行:
android:enabled=false
答案 1 :(得分:0)
也许这会对你有所帮助:
private SpannableString makeLinkSpan(CharSequence text, View.OnClickListener listener) {
SpannableString link = new SpannableString(text);
link.setSpan(new ClickableString(listener), 0, text.length(),
SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
return link;
}
private void makeLinksFocusable(TextView tv) {
MovementMethod m = tv.getMovementMethod();
if ((m == null) || !(m instanceof LinkMovementMethod)) {
if (tv.getLinksClickable()) {
tv.setMovementMethod(LinkMovementMethod.getInstance());
}
}
}
/*
* ClickableString class
*/
private static class ClickableString extends ClickableSpan {
private View.OnClickListener mListener;
public ClickableString(View.OnClickListener listener) {
mListener = listener;
}
@Override
public void onClick(View v) {
mListener.onClick(v);
}
}
示例:
private void spannableStringTEST()
{
String text = "SOME TEXT! HELLO! YEA";
String text2 = "Some other text.";
// Links
SpannableString link = makeLinkSpan("YOUR CLICKABLE TEXT", new View.OnClickListener() {
@Override
public void onClick(View v) {
// respond to click
Toast.makeText(mContext, "IT WORKS", Toast.LENGTH_SHORT).show();
//Intent intent = new Intent(Intent.ACTION_VIEW, "http://www.google.com");
//context.startActivity(intent);
}
});
TextView text = findViewByID(R.id.tv_cardView_text);
text .setText(text);
text .append(link);
text .append(text2);
makeLinksFocusable(text);
}
我的xml TextView:
<TextView
android:id="@+id/tv_cardView_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
希望你能用它做点什么!