Android 根据文本视图更改文本部分的颜色';s州
我有一堆文本,可能太长了,无法放在用户的屏幕上。因此,与其浪费空间,我只想显示它的一个简短摘录,并允许用户扩展它。此功能的设计要求在本节选的末尾有一小段彩色文本(“显示更多…”) 我想知道我怎样才能:Android 根据文本视图更改文本部分的颜色';s州,android,textview,Android,Textview,我有一堆文本,可能太长了,无法放在用户的屏幕上。因此,与其浪费空间,我只想显示它的一个简短摘录,并允许用户扩展它。此功能的设计要求在本节选的末尾有一小段彩色文本(“显示更多…”) 我想知道我怎样才能: 给这段文字涂上与其余文字不同的颜色 根据TextTiew的状态更改该文本位的颜色。如果用户未与其交互,则为蓝色;如果用户按下,则为红色 这可能吗?我该怎么做 注意:在某种意义上,我将把整个文本视图连接成一个按钮。一个文本视图中不同颜色的文本可以通过fromHTML方法实现。 用户对触摸状态的反
- 给这段文字涂上与其余文字不同的颜色
- 根据
的状态更改该文本位的颜色。如果用户未与其交互,则为蓝色;如果用户按下,则为红色TextTiew
注意:在某种意义上,我将把整个
文本视图连接成一个按钮。一个文本视图中不同颜色的文本可以通过fromHTML方法实现。
用户对触摸状态的反馈可以通过onTouchListener设置为textview来实现。
但在onTouchListener中,您需要编写代码来更改文本颜色。一个文本视图中不同颜色的文本可以通过fromHTML方法实现。
用户对触摸状态的反馈可以通过onTouchListener设置为textview来实现。
但在onTouchListener中,您需要编写代码来更改文本颜色。您可以像我一样通过覆盖省略号行为来扩展TextView(基于此问题的代码:)
导入android.content.Context;
导入android.graphics.Canvas;
导入android.text.Layout;
导入android.text.Layout.Alignment;
导入android.text.SpannableStringBuilder;
导入android.text.StaticLayout;
导入android.text.TextUtils;
导入android.text.TextUtils.TruncateAt;
导入android.util.AttributeSet;
导入android.widget.TextView;
导入java.util.ArrayList;
导入java.util.List;
公共类EllipsizingTextView扩展了TextView{
私有静态最终字符省略号='…';
公共接口EllipsizeListener{
void ellipsizeStateChanged(布尔ellipsized);
}
私有最终列表ellipsizeListeners=new ArrayList();
私有易失性布尔值已智能化;
私人故事;
私有易失性布尔编程更改;
私有字符序列fullCharSequence;
私有整数maxLines=-1;
专用浮点线间距乘法器=1.0f;
专用浮点线性加法垂直填充=0.0f;
公共ellipzingTextView(上下文){
超级(上下文);
}
公共EllipsizingTextView(上下文、属性集属性){
超级(上下文,attrs);
初始化属性(上下文、属性);
}
公共EllipsizingTextView(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
初始化属性(上下文、属性);
}
私有属性(上下文上下文、属性集属性){
maxLines=attrs.getAttributeIntValue(“http://schemas.android.com/apk/res/android“,”最大线“,-1);
如果(最大线!=-1){
isStale=正确;
}
}
public void addEllipsizeListener(EllipsizeListener侦听器){
if(侦听器==null){
抛出新的NullPointerException();
}
添加(监听器);
}
公共无效删除EllipsizeListener(EllipsizeListener侦听器){
删除(侦听器);
}
公共布尔值isEllipsized(){
返回是错误的;
}
公共void setMaxLines(int maxLines){
如果(getMaxLines()!=maxLines){
super.setMaxLines(maxLines);
this.maxLines=maxLines;
isStale=正确;
}
}
公共int getMaxLines(){
返回最大线;
}
公共void设置行间距(浮动添加、浮动多个){
this.lineAdditionalVerticalPadding=添加;
此.lineSpacingMultiplier=mult;
super.setlinespace(添加,多个);
}
受保护的void onTextChanged(字符序列文本、int-start、int-before、int-after){
super.onTextChanged(文本、开始、之前、之后);
如果(!programmaticChange){
fullCharSequence=文本;
isStale=正确;
}
}
受保护的void onDraw(画布){
如果(isStale){
super.setEllipsize(null);
重置文本();
}
super.onDraw(帆布);
}
私有void resetText(){
int maxLines=getMaxLines();
布尔椭圆化=假;
SpannableStringBuilder=SpannableStringBuilder.valueOf(fullCharSequence);
如果(最大线!=-1){
布局布局=createWorkingLayout(生成器);
if(layout.getLineCount()>maxLines){
int end=layout.getLineEnd(maxLines-1);
int-nextSpace=builder.toString().indexOf(“”,end);
如果(nextSpace!=-1){
delete(end,builder.length());
}
builder.append(省略号);
while(createWorkingLayout(builder).getLineCount()>maxLines){
int pos=builder.toString().lastIndexOf(“”);
delete(pos,builder.length());
builder.append(省略号);
}
省略=真;
}
}
如果(!TextUtils.equals(builder,getText())){
programmaticChange=true;
试一试{
setText(建筑商);
}最后{
programmaticChange=false;
}
}
isStale=假;
如果(省略!=已省略){
isEllipsized=ellipsized;
for(EllipsizeListener侦听器:ellipsizeListeners){
listener.ellipsizeStateChanged(ellipsized);
}
}
}
专用布局createWorkingLayout(CharSequence workingText){
返回新的StaticLayout(工作文本,getPaint(),getWidth()-getPaddingLeft()-getPaddi
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);
}
}