在你说它是转发之前,我不得不说我已经尝试过阅读其他帖子,所以请继续阅读。
在textView中显示链接的基本方法可以通过下一种方式完成:
final Spanned text = Html.fromHtml(getString(R.string.test));
textView.setText(text);
textView.setMovementMethod(LinkMovementMethod.getInstance());
并且string.xml文件可以包含:
<string name="test" formatted="false">
<![CDATA[
This<br />
is<br />
<<a href="http://www.google.com">a</a><br />
test
]]>
</string>
但是,我们可能希望捕获点击链接的事件,并自行处理。不仅如此,链接可能不需要是真正的网址。
很多帖子都是为了展示如何做到这一点,但没有一个我尝试过处理它。
有些让意图在应用程序上处理,这非常麻烦,可能意味着其他应用程序也可以处理它(对吧?),我认为它需要链接到特定模式。
有些人让LinkMovementMethod处理它,但破坏了链接的可点击性效果。有人说把颜色改成什么,但那不一样。
如何简单地在textview中添加一个监听器,告诉你哪个文本区域被单击?
目前,我使用过LinkMovementMethod。我的代码问题是:
textview.getLinkTextColors()。getDefaultColor()
这是代码:
MainActivity.java(如何使用的示例)
public class MainActivity extends Activity {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String a = "<a href='/a'>aaaa</a>123456<a href='/b'>bbbb</a>7890";
final TextView textView = (TextView) findViewById(R.id.span);
textView.setText(Html.fromHtml(a));
final LinkClickMovementMethodExt linkMovementMethod = new LinkClickMovementMethodExt();
linkMovementMethod.setOnLinkClickedListener(new OnLinkClickedListener() {
@Override
public void onLinkClicked(final TextView clickedTextView, final URLSpan clickedSpan) {
Toast.makeText(MainActivity.this, clickedSpan.getURL(), Toast.LENGTH_SHORT).show();
}
});
textView.setMovementMethod(linkMovementMethod);
}
}
LinkClickMovementMethodExt.java
/** a class to handle clicking on links of textViews . based on http://stackoverflow.com/a/16182500/878126 */
public class LinkClickMovementMethodExt extends LinkMovementMethod {
// TODO check how to get the default background of a clicked link
private final BackgroundColorSpan LINK_COLOR = new BackgroundColorSpan(0xFFACE0F4);
private final Class<URLSpan> spanClass = URLSpan.class;
private OnLinkClickedListener mOnLinkClickedListener;
public interface OnLinkClickedListener {
public void onLinkClicked(TextView textView, URLSpan clickedSpan);
}
@Override
public boolean onTouchEvent(final TextView textView, final Spannable buffer, final MotionEvent event) {
final int action = event.getAction();
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= textView.getTotalPaddingLeft();
y -= textView.getTotalPaddingTop();
x += textView.getScrollX();
y += textView.getScrollY();
final Layout layout = textView.getLayout();
final int line = layout.getLineForVertical(y);
final int off = layout.getOffsetForHorizontal(line, x);
/**
* get you interest span
*/
final Object[] spans = buffer.getSpans(off, off, spanClass);
if (spans.length != 0) {
if (action == MotionEvent.ACTION_DOWN) {
Selection.setSelection(buffer, buffer.getSpanStart(spans[0]), buffer.getSpanEnd(spans[0]));
for (final Object span : spans) {
if (span instanceof URLSpan) {
final int start = Selection.getSelectionStart(textView.getText());
final int end = Selection.getSelectionEnd(textView.getText());
final Spannable selectedSpan = (Spannable) textView.getText();
selectedSpan.setSpan(LINK_COLOR, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(selectedSpan);
if (mOnLinkClickedListener != null)
mOnLinkClickedListener.onLinkClicked(textView, (URLSpan) span);
}
}
return false;
} else if (action == MotionEvent.ACTION_UP) {
final Spannable span = (Spannable) textView.getText();
span.removeSpan(LINK_COLOR);
textView.setText(span);
return false;
}
}
}
return super.onTouchEvent(textView, buffer, event);
}
@Override
public boolean canSelectArbitrarily() {
return true;
}
@Override
public boolean onKeyUp(final TextView widget, final Spannable buffer, final int keyCode, final KeyEvent event) {
return false;
}
public void setOnLinkClickedListener(final OnLinkClickedListener onLinkClickedListener) {
this.mOnLinkClickedListener = onLinkClickedListener;
}
}
答案 0 :(得分:0)
在XML中:
<TextView
android:id="@+id/msgtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:linksClickable="true"
android:textColor="#000000"
android:textColorLink="#EE4E1D"
android:text="TextView" />
在Java中:
TextView tvmsg = (TextView)textEntryView.findViewById(R.id.msgtext);
tvmsg.setMovementMethod(LinkMovementMethod.getInstance());
tvmsg.setText(Html.fromHtml(message));