Closed LeronX closed 8 years ago
Use
List<Link> links = new ArrayList<>(); Link explore = new Link(Pattern.compile("#[0-9a-zA-Z\\u4e00-\\u9fa5_-]+")); explore.setTextColor(Color.parseColor("#f95548")); explore.setHighlightAlpha(0f); explore.setUnderlined(false); explore.setOnClickListener(new Link.OnClickListener() { @Override public void onClick(String clickedText) { } }); Link at = new Link(Pattern.compile("@[0-9a-zA-Z\\u4e00-\\u9fa5_-]+")); at.setTextColor(Color.parseColor("#f95548")); at.setHighlightAlpha(0f); at.setUnderlined(false); at.setOnClickListener(new Link.OnClickListener() { @Override public void onClick(String clickedText) { String nickname = clickedText.replace("@", ""); new BestAction().SearchAtUser(context,nickname); } }); links.add(explore); links.add(at); LinkBuilder.on(textView) .addLinks(links) .build();
EllipsizingTextView.java
import android.content.Context; import android.graphics.Canvas; import android.text.Layout; import android.text.Layout.Alignment; import android.text.StaticLayout; import android.text.TextUtils.TruncateAt; import android.util.AttributeSet; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class EllipsizingTextView extends TextView { private static final String ELLIPSIS = "...▼"; public interface EllipsizeListener { void ellipsizeStateChanged(boolean ellipsized); } private final List<EllipsizeListener> ellipsizeListeners = new ArrayList<EllipsizeListener>(); private boolean isEllipsized; private boolean isStale; private boolean programmaticChange; private String fullText; private int maxLines = -1; private float lineSpacingMultiplier = 1.0f; private float lineAdditionalVerticalPadding = 0.0f; public EllipsizingTextView(Context context) { super(context); } public EllipsizingTextView(Context context, AttributeSet attrs) { super(context, attrs); } public EllipsizingTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void setMaxLines(int maxLines) { super.setMaxLines(maxLines); this.maxLines = maxLines; isStale = true; } public int getMaxLines() { return maxLines; } @Override public void setLineSpacing(float add, float mult) { this.lineAdditionalVerticalPadding = add; this.lineSpacingMultiplier = mult; super.setLineSpacing(add, mult); } @Override protected void onTextChanged(CharSequence text, int start, int before, int after) { super.onTextChanged(text, start, before, after); if (!programmaticChange) { fullText = text.toString(); isStale = true; } } @Override protected void onDraw(Canvas canvas) { if (isStale) { super.setEllipsize(null); resetText(); } super.onDraw(canvas); } private void resetText() { int maxLines = getMaxLines(); String workingText = fullText; boolean ellipsized = false; if (maxLines != -1) { Layout layout = createWorkingLayout(workingText); if (layout.getLineCount() > maxLines) { workingText = fullText.substring(0, layout.getLineEnd(maxLines - 1)).trim(); while (createWorkingLayout(workingText + ELLIPSIS).getLineCount() > maxLines) { int lastSpace = workingText.lastIndexOf(' '); if (lastSpace == -1) { break; } workingText = workingText.substring(0, lastSpace - 1); } workingText = workingText + ELLIPSIS; ellipsized = true; } } if (!workingText.equals(getText())) { programmaticChange = true; try { setText(workingText); } finally { programmaticChange = false; } } isStale = false; if (ellipsized != isEllipsized) { isEllipsized = ellipsized; for (EllipsizeListener listener : ellipsizeListeners) { listener.ellipsizeStateChanged(ellipsized); } } } private Layout createWorkingLayout(String workingText) { return new StaticLayout(workingText, getPaint(), getWidth() - getPaddingLeft() - getPaddingRight(), Alignment.ALIGN_NORMAL, lineSpacingMultiplier, lineAdditionalVerticalPadding, false); } @Override public void setEllipsize(TruncateAt where) { // Ellipsize settings are not respected } }
Use
EllipsizingTextView.java