Selection.setSelection无法跨越



我正在尝试创建一个ListView,它的视图中有链接。就像推特应用程序一样。现在我就是这么做的:我在TextView中设置了android:autoLink="all"。

private static class TweetViewHolder {
    public LinkifiedTextView text;
    public TextView time;
    public ImageView authorImage;
}
holder.text.setText(post.getTweet().getText());
Pattern pattern = Pattern.compile("@+[a-zA-Z_]+");
Linkify.addLinks(holder.text, pattern, "http://www.twitter.com/", null, myTransformFilter);
pattern = Pattern.compile("#+[a-zA-Z_]+");
Linkify.addLinks(holder.text, pattern, "http://www.twitter.com/", null, myTransformFilter);
TransformFilter myTransformFilter = new TransformFilter() {
@Override
public String transformUrl(Matcher match, String url) {
 return url.substring(1); //remove the $ sign
}
};

这是我的"链接文本视图"代码:

@Override
public boolean onTouchEvent(MotionEvent event) {
  TextView widget = (TextView) this;
  Object text = widget.getText();
  if (text instanceof Spanned) {
      Spannable buffer = (Spannable) text;
      int action = event.getAction();
      if (action == MotionEvent.ACTION_UP
              || action == MotionEvent.ACTION_DOWN) {
          int x = (int) event.getX();
          int y = (int) event.getY();
          x -= widget.getTotalPaddingLeft();
          y -= widget.getTotalPaddingTop();
          x += widget.getScrollX();
          y += widget.getScrollY();
          Layout layout = widget.getLayout();
          int line = layout.getLineForVertical(y);
          int off = layout.getOffsetForHorizontal(line, x);
          ClickableSpan[] link = buffer.getSpans(off, off,
                  ClickableSpan.class);
          if (link.length != 0) {
              if (action == MotionEvent.ACTION_UP) {
                  link[0].onClick(widget);
              } else if (action == MotionEvent.ACTION_DOWN) {
                  Selection.setSelection(buffer,
                          buffer.getSpanStart(link[0]),
                          buffer.getSpanEnd(link[0]));
              }
              return true;
          }
      }
  }
  return false;

}

现在,代码正在压缩"可生成缓冲区=(可生成)文本;"为什么会这样,我该如何改变才能让它发挥作用?谢谢

因为并非所有的Spanneds都是Spanneables。Spanable是Spanned的一个子类,但不是唯一的子类。text对象是另一个子类。

相关内容

  • 没有找到相关文章

最新更新