简体   繁体   中英

Textview onclicklistener with links

I have a textview with a html string with anchors in it. When I click the textview I want to call eg a method called A, and when I click a link in the textview I want to call a method called B. I got this working but I got a problem: when I click a link, method B is called, but method A is called too. How can I make sure only method B, and not B and A, is called when I click a link?

My code:

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

        }
    });

Thanks in advance, Simon

I do the hack for you, try this code!

EXPLANATION:

1.use customized ClickableSpan to handle click on url.

2.clickablespan will handle the click event before the textview, make 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
    }

}

You could try modifying the onClickListener like mentioned here: 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

                             }

                    }
                });

Basically check where and when links start and end and only if you arent over the hyperlink, then call your method B , otherwise it anyway fires A on the links But this is just a workaround I guess

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

After studying the source code, I've concluded that there is no good way to do this, but probably your best choice is a custom movement method. Something like this:

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 a MyMovementMethod instead.

write this code in ClickableSpan

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM