can I change the look and feel of the clickablespan

和自甴很熟 提交于 2019-12-06 17:12:42

问题


I have to embed a clickable text in a paragraph text, clickablespan can do it. But instead of using the default focus/press/unfocus style, can I change those style ? Like when focus, the background color is blue, text color is white, when unfocus, the background color is white, text color is blue.


回答1:


Here is the example

bottomMsg = (TextView) findViewById(R.id.bottom_msg);
int start = bottomMsg.getText().toString().indexOf(terms);
MovementMethod movementMethod = LinkMovementMethod.getInstance();
bottomMsg.setMovementMethod(movementMethod);

Spannable text = (Spannable) bottomMsg.getText();
//TermAndConditions is a clickableSpan.
text.setSpan(new TermAndConditions(), start, start + terms.length(), Spannable.SPAN_POINT_MARK);
text.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.customYellowColor)), start, start + terms.length(), 0);

The point is, color-span is after the clickable-span. otherwise, the color not changed.




回答2:


You can override the updateDrawState method of ClickableSpan:

SpannableString spannableString = new SpannableString("text");
spannableString.setSpan(
    new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            Toast.makeText(getContext(), "Click!", Toast.LENGTH_LONG).show();
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(getResources().getColor(R.color.link));
        }
    }, 0, 4, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

textView.setText(spannableString);

textView.setMovementMethod(LinkMovementMethod.getInstance());



回答3:


Link color can be changed in xml:

<TextView
    android:textColor="..."
    android:textColorLink="..." />


来源:https://stackoverflow.com/questions/4282975/can-i-change-the-look-and-feel-of-the-clickablespan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!