添加标签到ClickableSpan



我试图在ClickableSpan中存储数据,就像它可以用setTag()与其他类型的元素一样。

我在stackoverflow中读过这个答案:SpannableString - setTag

它清楚地说:-创建你自己的子类ClickableSpan保存你想要的数据,并将其应用到你的SpannableString。-当你创建ClickableSpan并通过数据成员将一个单词附加到它时,你可以在onClick()

中访问该数据

但是我是个新手,不知道怎么写这个。

我正在使用一个类扩展clickableespan来创建我的可点击跨度:

// CLASS BEING IMPORTED
public abstract class TouchableSpan extends ClickableSpan {
    // IMPLEMENT SET TAG FUNCTION HERE??
}
// CODE ON ACTIVITY
TouchableSpan touchableSpan = new TouchableSpan() {
    @Override
    public void onClick(View widget) {
        this.setPressed(true);
        // GET TAG INFO HERE. BUT HOW?
    }
    // SET TAG HERE? BUT HOW?
    private boolean mIsPressed;
    public void setPressed(boolean isSelected) {
        mIsPressed = isSelected;
    }
    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setColor(Color.BLACK);
        ds.bgColor = mIsPressed ? selectedHlColor : 0xffeeeeee;
        ds.setUnderlineText(false);
    }
}
spannablesstringbuilder.setSpan(touchableSpan, index+2, index2, 0);

评论:我知道我应该在我链接的问题的评论部分要求澄清,但我没有足够的声誉来这样做。

我认为解决方案是在自定义类中使用自定义函数像这样的东西可以工作

// CLASS BEING IMPORTED
public abstract class TouchableSpan extends ClickableSpan {
  // IMPLEMENT SET TAG FUNCTION HERE??
  //COMMENT: YES, LIKE THIS: CAN BE ANY FUNCTION U LIKE
  private String myCustomVar;
  public String getMyVar(){
  return this.myCustomVar;
  }
  public void setMyVar(String myVari){
  this.myCustomVar = myVari;
  return;
  }
}
// CODE ON ACTIVITY
TouchableSpan touchableSpan = new TouchableSpan() {
  @Override
  public void onClick(View widget) {
      this.setPressed(true);
      // GET TAG INFO HERE. BUT HOW?
      //COMMENT: LIKE THIS: 
      String extravar = touchableSpan.getMyVar();
      Log.d("TEST", extravar);
  }
  // SET TAG HERE? BUT HOW?
  COMMENT: NOT HERE. SEE BELOW
  private boolean mIsPressed;
  public void setPressed(boolean isSelected) {
      mIsPressed = isSelected;
  }
  @Override
  public void updateDrawState(TextPaint ds) {
      super.updateDrawState(ds);
      ds.setColor(Color.BLACK);
      ds.bgColor = mIsPressed ? selectedHlColor : 0xffeeeeee;
      ds.setUnderlineText(false);
  }
}
//COMMENT: SET YOUR VARIABLE/TAG HERE:
touchableSpan.setMyVar("HOLALA");
spannablesstringbuilder.setSpan(touchableSpan, index+2, index2, 0);

像这样定义你的类:

    public abstract class TouchableSpan extends ClickableSpan {
    private Object mTag;
    public void setTag(Object tag) {
        mTag = tag;
    }
    public Object getTag() {
        return mTag;
    }   
    @Override
    public void onClick(View widget) {
        this.setPressed(true);
        // GET TAG INFO HERE. BUT HOW?
        // Just access mTag here
    }
    private boolean mIsPressed;
    public void setPressed(boolean isSelected) {
        mIsPressed = isSelected;
    }
    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setColor(Color.BLACK);
        ds.bgColor = mIsPressed ? selectedHlColor : 0xffeeeeee;
        ds.setUnderlineText(false);
    }
}

然后像这样创建你的TouchableSpan:

TouchableSpan touchableSpan = new TouchableSpan();
touchableSpan.setTag("tag"); // you can use any Object here, e.g. a String
spannablesstringbuilder.setSpan(touchableSpan, index+2, index2, 0);

你也可以使用你原来的方法,扩展clickableespan两次,但这可能更干净,你可以在其他活动或项目中重用你的touchableespan类

相关内容

  • 没有找到相关文章

最新更新