当链接被触摸时,我需要我的ClickableSpan来更改链接背景。这是我的代码:
holder.text.setMovementMethod(LinkMovementMethod.getInstance());
holder.text.setText(addClickablePart(post.getTweet().getText()), BufferType.SPANNABLE);
private SpannableStringBuilder addClickablePart(String str) {
SpannableStringBuilder ssb = new SpannableStringBuilder(str);
int idx1 = str.indexOf("#");
int idx2 = 0;
while (idx1 != -1) {
idx2 = str.indexOf(" ", idx1);
if(idx2 == -1) {
idx2 = str.length();
}
final String clickString = str.substring(idx1, idx2);
ssb.setSpan(new ClickableSpan() {
@Override
public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
super.updateDrawState(ds);
}
@Override
public void onClick(View widget) {
if(clickString.startsWith("#")) {
Toast.makeText(getView().getContext(), "left",
Toast.LENGTH_SHORT).show();
}
}
}, idx1, idx2, 0);
idx1 = str.indexOf("#", idx2);
}
我不知道我怎么能成功。。。有什么想法吗?
尝试使用如下的后台xml:放在你的抽屉里并设置在您的微调器中:
android:background="@drawable/YOUR_XML_FILE"
xml文件
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape
///WHEN PRESSED TO CODE HERE WILL BE ACTIVATED///
</shape>
</item>
<item>
<shape
////YOU CODE FOR WHEN NOT PRESSED///
</shape>
</item>
</selector>