1

我有一个带有 html 文本的 TextView,我需要本机复制/粘贴和可点击链接。

我已经使用了下一个代码,但是当我使用 setMovementMethod(LinkMovementMethod.getInstance()); 本机复制/粘贴停止工作。

如果我将 setMovementMethod(LinkMovementMethod.getInstance()) 更改为 ArrowKeyMovementMethod,则复制/粘贴有效,但点击链接停止工作。

我没有发布所有代码,但 setMovementMethod 用于 updateDetail 方法。

有人可以帮助我吗?问候,路易斯

我的代码是:

textDetail = (TextView) view.findViewById(R.id.text_detail);
textDetail.setTextIsSelectable(true);
textDetail.setCustomSelectionActionModeCallback(new ActionMode.Callback()
 {
   public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    return false;
   }

  public void onDestroyActionMode(ActionMode mode) {
                             // TODO Auto-generated method stub

  }

  public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                             // TODO Auto-generated method stub
     return true;
  }

  public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                             // TODO Auto-generated method stub
      return false;
     }
   });

  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) {
                             } 
                               ////////
                            else {
                               ///////////////
                             }
                         }
                     };
                     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);
                 }

    public void updateDetail(String msg) {
               setTextViewHTML(textDetail, msg);
               textDetail.setMovementMethod(LinkMovementMethod.getInstance());
             }
4

0 回答 0