[英]Android: Prevent changing background color of text in TextView with ClickableSpan when touched
[英]Android: Change the background color of a ClickableSpan when clicked
我有一個帶有ClickableSpan的SpannableString,如下所示
for (int i = 0; i < items.size(); i++) {
final SpannableString span = new SpannableString(items.get(i));
final int index=i;
span.setSpan(new ClickableSpan() {
@Override
public void onClick(View widget) {
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setColor(Color.LTGRAY);
ds.setUnderlineText(false);
}
}, 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(span);
}
//my text view
txt.setText(builder);
txt.setMovementMethod(LinkMovementMethod.getInstance());
我想要做的是在點擊時更改跨度的前景色。
我怎樣才能做到這一點 ?
如果你想要擺脫選擇上的綠色亮點,這就是你想要知道的:
顯然,在自定義類中覆蓋public void updateDrawState(TextPaint ds)
不會影響突出顯示顏色。 它僅用於設置下划線顏色(或隱藏/顯示它)。
您需要做的就是: textView.setHighlightColor(Color.TRANSPARENT);
textView是包含ClickableSpan的內容。
希望它適用於所有人。 隨意提出任何相關問題。
我擴展了clickableSpan類並向它傳遞了一個標志,讓我知道我應該突出顯示它。
SpannableStringBuilder tag;
.... tag.setSpan(new WordSpan(i, tokens[i], wordtohighlitedID) {
.....
import android.graphics.Color;
import android.text.TextPaint;
import android.text.style.ClickableSpan;
import android.view.View;
public class WordSpan extends ClickableSpan
{
private int id;
private TextPaint textpaint;
public boolean shouldHilightWord = false;
public WordSpan(int anID, String txt, int selected) {
id =anID;
// if the word selected is the same as the ID set the highlight flag
if(selected == id) {
shouldHilightWord = true;
}
}
@Override
public void updateDrawState(TextPaint ds) {
textpaint = ds;
ds.setColor(ds.linkColor);
if(shouldHilightWord){
textpaint.bgColor = Color.GRAY;
textpaint.setARGB(255, 255, 255, 255);
}
//Remove default underline associated with spans
ds.setUnderlineText(false);
}
public void changeSpanBgColor(View widget){
shouldHilightWord = true;
updateDrawState(textpaint);
widget.invalidate();
}
@Override
public void onClick(View widget) {
// TODO Auto-generated method stub
}
/**
* This function sets the span to record the word number, as the span ID
* @param spanID
*/
public void setSpanTextID(int spanID){
id = spanID;
}
/**
* Return the wordId of this span
* @return id
*/
public int getSpanTextID(){
return id;
}
}
用這個:
view.setSelector(new ColorDrawable(Color.BLUE));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.