Android:使用SpannableString的链接失败(背景颜色很奇怪)

时间:2016-01-15 14:38:38

标签: android textview spannablestring

我希望在TextView中有一个链接。所以我使用SpannableString;但是当我点击特定文字时,有一个绿色背景的区域?

private void makeLinkSpan(SpannableString spannablestring, int start, int end,
                          View.OnClickListener onclicklistener) {
    Resources resources = getResources();
    spannablestring.setSpan(new ClickableString(onclicklistener,
            resources.getColor(R.color.white), resources.getColor(R.color.white),
            resources.getColor(R.color.link_normal_2_text),
            resources.getColor(R.color.link_pressed_text)), start, end, Spanned.SPAN_MARK_MARK);
}

使用:

  • link_normal_2_text:是灰色的
  • link_pressed_text:红色

TextView:

<TextView
        android:id="@+id/send_by_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal"
        android:layout_marginBottom="@dimen/keyline_1"
        android:layout_marginLeft="@dimen/keyline_1"
        android:layout_marginRight="@dimen/keyline_1"
        style="@style/MyStyle"/>

myStyle的:

<style name="MyStyle">
    <item name="android:textSize">@dimen/text_size_medium</item>
    <item name="android:fontFamily">sans-serif-light</item>
    <item name="android:textColor">@color/text_1_secondary_color</item>
</style>

使用:

  • text_1_secondary_color:是另一个灰色

我的ClickableString类:

public class ClickableString extends ClickableSpan {
private boolean mIsPressed;
private View.OnClickListener mListener;
private int mNormalBackgroundColor;
private int mNormalTextColor;
private int mPressedBackgroundColor;
private int mPressedTextColor;

public ClickableString(View.OnClickListener onclicklistener, int normalBackgroundColor,
                       int pressedBackgroundColor, int normalTextColor, int pressedTextColor) {
    mListener = onclicklistener;
    mPressedBackgroundColor = pressedBackgroundColor;
    mNormalBackgroundColor = normalBackgroundColor;
    mNormalTextColor = normalTextColor;
    mPressedTextColor = pressedTextColor;
}

public void onClick(View view) {
    mListener.onClick(view);
}

public void setPressed(boolean flag) {
    mIsPressed = flag;
}

public void updateDrawState(TextPaint textpaint) {
    int textColor;
    boolean flag;

    if (mIsPressed) {
        textColor = mPressedTextColor;
    } else {
        textColor = mNormalTextColor;
    }
    textpaint.setColor(textColor);

    if (mIsPressed) {
        textColor = mPressedBackgroundColor;
    } else {
        textColor = mNormalBackgroundColor;
    }
    textpaint.bgColor = textColor;

    if (!mIsPressed) {
        flag = true;
    } else {
        flag = false;
    }

    textpaint.setUnderlineText(flag);
}

}

当我点击第二部分(字符串可点击)时,这是结果:

enter image description here

0 个答案:

没有答案