简体   繁体   中英

Highlight on clickablespan click

I've got little problem, i need to remove or customize this orange highlight during clicking on clickablespan. This is my class extending ClickableSpan

public class InternalClickableSpan extends ClickableSpan {

    private String clicked;

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setUnderlineText(false);
    }

    public InternalClickableSpan(String clickedString) {
        clicked = clickedString;
    }

    @Override
    public void onClick(View view) {
        Selection.setSelection((Spannable) ((TextView)view).getText(), 0);
        Toast toast = Toast.makeText(mContext, clicked, Toast.LENGTH_SHORT);
        toast.show();
    }
}

and this is how i use it on text view

Spannable spans = (Spannable) tv.getText();      
spans.setSpan(new InternalClickableSpan(contacts[i]), text.indexOf(contacts[i]),   text.indexOf(contacts[i])+contacts[i].length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Does anybody know how to customize "onclick highlight" on spannable object?

edit: Thanks Aleadam for response, i'am overriding updateDrawState (please take a look at the first method in my InternalClickableSpan class), but i can't find a way to customize this higlight anyway. Has anybody got other ideas? Thanks

You can override onClick(View widget) like this:

        @Override
        public void onClick(View widget) {
            // do what must happen after click event.
            widget.invalidate();
        }

这将删除任何突出显示。

tv.setHighlightColor(Color.TRANSPARENT);

在此处输入图片说明

ClickableSpan linkClick = new ClickableSpan() {
    @Override
    public void onClick(View view) {
        Toast.makeText(getApplicationContext(), "Link Click",
                Toast.LENGTH_SHORT).show();
        view.invalidate();
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        if (textView.isPressed()) {
            ds.setColor(Color.BLUE);
        } else {
            ds.setColor(Color.RED);
        }
        textView.invalidate();
    }
};
textView.setHighlightColor(Color.TRANSPARENT);

Spannable spannableString = new SpannableString("Link in TextView");
spannableString.setSpan(linkClick, 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString, TextView.BufferType.SPANNABLE);
textView.setMovementMethod(LinkMovementMethod.getInstance());

就用这个..

view.setSelector(new ColorDrawable(Color.TRANSPARENT));

u can replace default highlightColor android:textColorHighlight

    <TextView
    android:id="@+id/tv_tip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#727998"
    android:textColorHighlight="@android:color/transparent"
    tools:text="@string/_tip" />

or disable focus

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout android:id="@+id/LinearLayout02"
        android:layout_height="50px"
        android:layout_width="fill_parent"
        // Layout Click enable
        android:clickable="true"
        // Setting Highlight Option in background property
        android:background="@android:drawable/list_selector_background" />
    </LinearLayout>
</LinearLayout>
textView.setText(myString);
Linkify.addLinks(textView,Linkify.ALL);

This works for me.

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