我有一堆文字可能太长而无法放在用户的屏幕上。因此,我不想浪费空间,只想展示它的简短摘录,并允许用户扩展它。此功能的设计需要在此摘录的末尾添加一小段彩色文本(“显示更多...”)。
我想知道我怎么做:
TextTiew
的状态更改该位文本的颜色。如果用户没有与之交互则为蓝色,而在用户按下时则为红色。这可能吗?我将如何去做?
注意:在某种意义上,我将整个TextView
作为按钮连接起来。
答案 0 :(得分:2)
您可以使用覆盖的椭圆形行为来扩展TextView,就像我已经完成的那样(基于此问题的代码:android ellipsize multiline textview)
import android.content.Context;
import android.graphics.Canvas;
import android.text.Layout;
import android.text.Layout.Alignment;
import android.text.SpannableStringBuilder;
import android.text.StaticLayout;
import android.text.TextUtils;
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 char ELLIPSIS = '…';
public interface EllipsizeListener {
void ellipsizeStateChanged(boolean ellipsized);
}
private final List<EllipsizeListener> ellipsizeListeners = new ArrayList<EllipsizeListener>();
private volatile boolean isEllipsized;
private volatile boolean isStale;
private volatile boolean programmaticChange;
private CharSequence fullCharSequence;
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);
initAttributes(context, attrs);
}
public EllipsizingTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initAttributes(context, attrs);
}
private void initAttributes(Context context, AttributeSet attrs) {
maxLines = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "maxLines", -1);
if (maxLines != -1) {
isStale = true;
}
}
public void addEllipsizeListener(EllipsizeListener listener) {
if (listener == null) {
throw new NullPointerException();
}
ellipsizeListeners.add(listener);
}
public void removeEllipsizeListener(EllipsizeListener listener) {
ellipsizeListeners.remove(listener);
}
public boolean isEllipsized() {
return isEllipsized;
}
public void setMaxLines(int maxLines) {
if (getMaxLines() != maxLines) {
super.setMaxLines(maxLines);
this.maxLines = maxLines;
isStale = true;
}
}
public int getMaxLines() {
return maxLines;
}
public void setLineSpacing(float add, float mult) {
this.lineAdditionalVerticalPadding = add;
this.lineSpacingMultiplier = mult;
super.setLineSpacing(add, mult);
}
protected void onTextChanged(CharSequence text, int start, int before, int after) {
super.onTextChanged(text, start, before, after);
if (!programmaticChange) {
fullCharSequence = text;
isStale = true;
}
}
protected void onDraw(Canvas canvas) {
if (isStale) {
super.setEllipsize(null);
resetText();
}
super.onDraw(canvas);
}
private void resetText() {
int maxLines = getMaxLines();
boolean ellipsized = false;
SpannableStringBuilder builder = SpannableStringBuilder.valueOf(fullCharSequence);
if (maxLines != -1) {
Layout layout = createWorkingLayout(builder);
if (layout.getLineCount() > maxLines) {
int end = layout.getLineEnd(maxLines - 1);
int nextSpace = builder.toString().indexOf(' ', end);
if (nextSpace != -1) {
builder.delete(end, builder.length());
}
builder.append(ELLIPSIS);
while (createWorkingLayout(builder).getLineCount() > maxLines) {
int pos = builder.toString().lastIndexOf(' ');
builder.delete(pos, builder.length());
builder.append(ELLIPSIS);
}
ellipsized = true;
}
}
if (!TextUtils.equals(builder, getText())) {
programmaticChange = true;
try {
setText(builder);
} finally {
programmaticChange = false;
}
}
isStale = false;
if (ellipsized != isEllipsized) {
isEllipsized = ellipsized;
for (EllipsizeListener listener : ellipsizeListeners) {
listener.ellipsizeStateChanged(ellipsized);
}
}
}
private Layout createWorkingLayout(CharSequence workingText) {
return new StaticLayout(workingText, getPaint(), getWidth() - getPaddingLeft() - getPaddingRight(),
Alignment.ALIGN_NORMAL, lineSpacingMultiplier, lineAdditionalVerticalPadding, false);
}
public void setEllipsize(TruncateAt where) {
// Ellipsize settings are not respected
}
public int getLineCount() {
return Math.min(super.getLineCount(), getMaxLines());
}
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
if (changed) {
isStale = true;
}
super.onLayout(changed, left, top, right, bottom);
}
}
您可以用字符串替换ELLIPSIS(如“显示更多”),并为此部分文本添加ForegroundColorSpan或BackgroundColorSpan。在这种情况下要小心空格(它会导致无限循环)。
答案 1 :(得分:2)
答案 2 :(得分:0)
一个textview中的不同颜色的文本可以通过HTML方法实现。 并且可以通过设置为textview的onTouchListener来实现用户对触摸状态的反馈。 但是在onTouchListener中,您需要编写使更改文本颜色的代码。