简体   繁体   中英

Android: How to make Linkify links not long-clickable

I have some TextViews that I use Linkify on. I'd like for the links to not be long-clickable. Using setLongClickable(false) on the TextViews doesn't have any affect on the Linkify links. Is there any way to make these links not long-clickable?

this will be helpful to you

Spannable span = Spannable.Factory.getInstance().newSpannable("my span text"); 
             Linkify.addLinks(span, Linkify.ALL);
         URLSpan[] uspans = span.getSpans(0, mTextAdData.length(), URLSpan.class);

    int i = 0;
            for (URLSpan uspan : uspans) {
                int start = span.getSpanStart(uspan);
                int end = span.getSpanEnd(uspan);
                span.removeSpan(uspan);
                MyUrlSpan uspan1 = new MyUrlSpan(uspan.getURL());
                span.setSpan(uspan1, start, end, 0);
                if (i != start) {
                    // From i to start of this span set a custom span
                    span.setSpan(new MyUrlSpan(), i, start,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
                i = end;
            }

    public class MyUrlSpan extends URLSpan implements OnLongClickListener {
                public MyUrlSpan(String string) {
                    super(string);
                }

                       @Override
                       public void onClick(View v){
                       }

                       @Override
                public boolean onLongClick(View v) {
                    return false;
                }
        }

Turns out, the solution was quite simple:

textview.setOnLongClickListener(new OnLongClickListener(){
    public boolean onLongClick(View v) {
        return true;
    }
});

try this...

    Link.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            Link.setLongClickable(false);
            return false;
        }
    });

    Link.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (!Link.isLongClickable()) {
                Link.setLongClickable(true);
                return;
            }

            //  Link your page 
        }
    });

Here is a little class I wrote for this case:

public class NoLongClickMovementMethod extends LinkMovementMethod {

long longClickDelay = ViewConfiguration.getLongPressTimeout();
long startTime;

private static NoLongClickMovementMethod linkMovementMethod = new NoLongClickMovementMethod();

@Override
public boolean onTouchEvent(android.widget.TextView widget, android.text.Spannable buffer, MotionEvent event) {
    int action = event.getAction();

    if (action == MotionEvent.ACTION_DOWN) {
        startTime = System.currentTimeMillis();
    }

    if (action == MotionEvent.ACTION_UP) {
        long currentTime = System.currentTimeMillis();
        if (currentTime - startTime >= longClickDelay)
            return true;
    }
    return super.onTouchEvent(widget, buffer, event);
}

public static android.text.method.MovementMethod getInstance() {
    return linkMovementMethod;
}

Usage: textView.setMovementMethod(NoLongClickMovementMethod.getInstance());

If you need it to be still clickable but not long-clickable, consider overload activity's dispatchTouchEvent to listen for ACTION_UP and set setOnTouchListener in your TextView to listen for ACTION_DOWN . Then just route these events directly into MovementMethod .

This way you'll be able to only get clicks for your links while having the text behavior correct in all other cases (background onClick animation, clickable in other textview areas, etc).

Yeah, don't forget to call textView.setMovementMethod(null) when installing the 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