简体   繁体   中英

Add tag to ClickableSpan

I'm trying to store data in a ClickableSpan, like it can be done with setTag() with other type of elements.

I've read this answer in stackoverflow: SpannableString - setTag

And it clearly says: - Create your own subclass of ClickableSpan that holds the data you want, and apply it to your SpannableString. - When you create the ClickableSpan and attach a word to it via a data member, you will have access to that data in onClick()

But I'm somewhat of a newbie and can't figure out how to code this.

I am using a class that extends ClickableSpan to create my clickable spans:

// 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);

comment: I know I should ideally ask clarification on the comments sections of the question I linked, but I don't have enough reputation to do so.

I think that the solution is use custom functions inside your custom class somthing like this could work

// 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);

Define your class like this:

    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);
    }
}

Then create your TouchableSpan like this:

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

You can use your original approach too and extend ClickableSpan twice but this is probably cleaner and you can re-use your TouchableSpan class in other Activities or projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM