Textview onclicklistener with links

I have a textview with an html string with anchors in it. When I click on the text view that I want to call, for example, method A, and when I click on the link in text form, I want to call method B. I got this, but I have a problem: when I click on the link, method B is called, but method A is also called. How can I make sure that when a link to a link is clicked, only method B is called, not B and A?

My code is:

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()); 

And the onclicklistener text box:

 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(); } } }); 

Thanks in advance, Simon.

+6
source share
4 answers

I am doing a hack for you, try this code!

EXPLANATION:

1. Dependent custom ClickableSpan to process, click on the URL.

2.clickablespan handles the click event before text viewing, makes a flag when a link is clicked.

 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 } } 
+9
source

You can try changing the onClickListener as indicated here: Control onclicklistener in autostart text

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

Basically check where and when links start and end, and only if you are not a hyperlink, then call your method B, otherwise it starts A through the links anyway But this is just a workaround, I think

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

+5
source

After examining the source code, I came to the conclusion that there is no good way to do this, but probably your best bet is the user movement method. Something like that:

 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); } } 

Then, instead of calling LinkMovementMethod.getInstance() create MyMovementMethod instead.

0
source

write this code in ClickableSpan

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

Source: https://habr.com/ru/post/949899/


All Articles