2013-07-20 79 views
6

我有一个带有锚点的html字符串的textview。当我点击textview时,我想调用一个名为A的方法,当我在textview中单击一个链接时,我想调用一个名为B的方法。我得到了这个工作,但我遇到了一个问题:当我单击一个链接时,方法B被调用,但方法A也被调用。我怎样才能确保只有方法B,而不是B和A,当我点击链接时被调用?带有链接的Textview onclicklistener

我的代码:

for (int i = 0; i < ingevoegd.length(); i++) { 
     JSONObject soortingevoegd = ingevoegd.getJSONObject(i); 
     String type = soortingevoegd.getString("type");   
     if (type.equals("Vis")) { 
      String link = "<a href = 'com.aquariumzoeken.pro://Soortweergave?selected=" 
        + naam + "&type=Vis" + "'>" + naam + "</a>"; 
      text = text.replaceAll(naam, link); 
     } 
    } 

    TextView texttv = (TextView) v.findViewById(R.id.textviewer); 


    texttv.setText(Html.fromHtml(text)); 
    texttv.setMovementMethod(LinkMovementMethod.getInstance()); 

而且TextView的onclicklistener:

texttv.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      try { 

       switchToEditMode sw = new switchToEditMode(); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
    }); 

由于提前, 西蒙

+0

您是否尝试过让'机器人:自动链接= “网络” 为'TextView的和不附加LinkMovement?我认为这应该工作。 – Slartibartfast

+0

不幸的是,这不起作用。我的链接不能以这种方式点击。 – Simon

+0

你可以附上一张截图吗,我发现很难想象布局以及为什么附加两个事件,电视中其他地方是否有空白空间,以及那些你需要的方法B? – Slartibartfast

回答

5

喜欢这里提到的你可以尝试修改onClickListener:Control onclicklistener in autolink enabled textview

texttv.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
          if(texttv.getSelectionStart()==-1&&texttv.getSelectionEnd()==-1){ 

         // Method B 

          } 

        } 
       }); 

基本上检查何时何环节开始和结束,只有当你的arent在超链接,然后打电话给你的方法B,否则无论如何会触发上的链接 但是,这只是一种变通方法我想在这里

文档:http://developer.android.com/reference/android/text/Selection.html#getSelectionEnd(java.lang.CharSequence)

+1

也不起作用。当单击textview时不调用方法B – Simon

+0

看看这个如果它没有工作http://stackoverflow.com/a/41603171/5873832 –

0

研究完源代码后,我断定没有好的方法来做到这一点,但可能你最好的选择是自定义移动方法。事情是这样的:

class MyMovementMethod extends LinkMovementMethod { 

    // Most of this code copied from LinkMovementMethod 
    @Override 
    public boolean onTouchEvent(TextView widget, Spannable buffer, 
           MotionEvent event) { 
     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; 
      } else { 
       Selection.removeSelection(buffer); 
       // NEW CODE - call method B 
       B(); 
      } 
     } 

     return Touch.onTouchEvent(widget, buffer, event); 
    } 

} 

然后,而不是调用LinkMovementMethod.getInstance(),创建MyMovementMethod代替。

8

我为你做了破解,试试这段代码吧!

说明:

1.使用定制ClickableSpan处理点击网址。

2.clickablespan将处理textview之前的点击事件,并在点击链接时生成一个标志。

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    TextView textView = (TextView) findViewById(R.id.main_text); 
    textView.setMovementMethod(LinkMovementMethod.getInstance()); 

    CharSequence charSequence = textView.getText(); 
    SpannableStringBuilder sp = new SpannableStringBuilder(charSequence); 

    URLSpan[] spans = sp.getSpans(0, charSequence.length(), URLSpan.class); 

    for (URLSpan urlSpan : spans) { 
     MySpan mySpan = new MySpan(urlSpan.getURL()); 
     sp.setSpan(mySpan, sp.getSpanStart(urlSpan), 
       sp.getSpanEnd(urlSpan), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); 
    } 

    textView.setText(sp); 

    textView.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // 2.if clicking a link 
      if (!isClickingLink) { 
       Log.w("log", "not clicking link"); 
      } 
      isClickingLink = false; 
     } 
    }); 
} 


private boolean isClickingLink = false; 

private class MySpan extends ClickableSpan { 

    private String mUrl; 

    public MySpan(String url) { 

     super(); 
     mUrl = url; 
    } 

    @Override 
    public void onClick(View widget) { 

     isClickingLink = true; 
     // 1. do url click 
    } 

} 
+0

工作就像一个魅力。谢谢!我只需在我的TextView xml和MySpan onClick中添加android:autoLink =“web | phone | email”,我不得不添加一个转到url的意图。 –

+0

非常感谢这对我有用 – Pruthviraj

+0

我试过这个,我得到错误:“URLSpan:Actvity没有找到意图”。 – trans

0

写这段代码ClickableSpan

ClicableSpan.onClick(View v){ 
    yourTextView.setOnClickListener(null); 
    /* do your own click 
    */ 
    yourTextView.setOnClickListener(your listener); 
}