Skip to content

Commit

Permalink
重构span点击事件,支持更多属性获取
Browse files Browse the repository at this point in the history
  • Loading branch information
JinJieGu committed Jan 18, 2018
1 parent bca95a3 commit 73e4ded
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
13 changes: 10 additions & 3 deletions app/src/main/java/cn/iwgang/simplifyspandemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import cn.iwgang.simplifyspan.SimplifySpanBuild;
import cn.iwgang.simplifyspan.customspan.CustomClickableSpan;
import cn.iwgang.simplifyspan.other.OnClickableSpanListener;
import cn.iwgang.simplifyspan.other.SpecialConvertMode;
import cn.iwgang.simplifyspan.other.SpecialGravity;
import cn.iwgang.simplifyspan.unit.SpecialClickableUnit;
import cn.iwgang.simplifyspan.unit.SpecialImageUnit;
Expand Down Expand Up @@ -155,8 +158,12 @@ protected void onCreate(Bundle savedInstanceState) {
}

@Override
public void onClick(TextView tv, String clickText, Object tag) {
Toast.makeText(MainActivity.this, "Click Text: " + clickText +" tag: " + tag, Toast.LENGTH_SHORT).show();
public void onClick(TextView tv, CustomClickableSpan clickableSpan) {
Toast.makeText(MainActivity.this, "Click Text: " + clickableSpan.getClickText()
+ " tag: " + clickableSpan.getTag()
+ " StartSpanIndex: " + clickableSpan.getStartSpanIndex()
+ " EndSpanIndex: " + clickableSpan.getEndSpanIndex()
, Toast.LENGTH_SHORT).show();
}

private float sp2px(float spValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ public class CustomClickableSpan extends ClickableSpan {
private int mBgColorNor;
private int mBgColorPre;
private Object tag;
private String clickText;
private int startSpanIndex;
private int endSpanIndex;

public CustomClickableSpan(SpecialClickableUnit specialClickableUnit) {
tag = specialClickableUnit.getTag();
setTag(specialClickableUnit.getTag());
mTextColorNor = specialClickableUnit.getNormalTextColor();
mTextColorPre = specialClickableUnit.getPressTextColor();
mBgColorNor = specialClickableUnit.getNormalBgColor();
Expand All @@ -46,9 +49,10 @@ public void onClick(View widget) {
if (null != mOnClickableSpanListener) {
TextView tv = (TextView)widget;
Spanned spanned = (Spanned)tv.getText();
int start = spanned.getSpanStart(this);
int end = spanned.getSpanEnd(this);
mOnClickableSpanListener.onClick(tv, spanned.subSequence(start, end).toString(), tag);
startSpanIndex = spanned.getSpanStart(this);
endSpanIndex = spanned.getSpanEnd(this);
clickText = spanned.subSequence(startSpanIndex, endSpanIndex).toString();
mOnClickableSpanListener.onClick(tv, this);
}
}

Expand Down Expand Up @@ -86,4 +90,28 @@ public void updateDrawState(TextPaint ds) {
ds.setUnderlineText(false);
}
}

public Object getTag() {
return tag;
}

public void setTag(Object tag) {
this.tag = tag;
}

//get current click text
public String getClickText(){
return clickText;
}

//get current click text span start index in total
public int getStartSpanIndex() {
return startSpanIndex;
}

//get current click text span start index in end
public int getEndSpanIndex() {
return endSpanIndex;
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package cn.iwgang.simplifyspan.other;

import android.text.style.ClickableSpan;
import android.widget.TextView;

import cn.iwgang.simplifyspan.customspan.CustomClickableSpan;

/**
* ClickableSpan Listener
* Created by iWgang on 15/12/3.
* https://github.com/iwgang/SimplifySpan
*/
public interface OnClickableSpanListener {

void onClick(TextView tv, String clickText, Object tag);
void onClick(TextView tv, CustomClickableSpan clickableSpan);

}

0 comments on commit 73e4ded

Please sign in to comment.