Android TextView.BufferType枚举类型

标签:
androidtextviewbuffer杂谈 |
TextView.BufferType枚举值:NORMAL, SPANNABLE, EDITABLE
常常使用setText(CharSequence text)为TextView赋值。
但是你有没有注意到,textview还有个方法:
public void setText(CharSequence text, BufferType
type)
注意到:
TextView 控件设置文本时的参数有TextView.BufferType.EDITABLE 和 TextView.BufferType.SPANNABLE
但是不知道具体啥差别, 于是决定窥探 Google 代码,看个究竟。
一、在 TextView 类中找到 setText 方法关于 BufferType 的主要代码:
Java代码 http://litonggang.iteye.com/images/icon_star.pngTextView.BufferType枚举类型" />
可以看出根据不同 BufferType ,用类工厂的模式创建控件,那么为什么不直接 new 一个控件呢?首先想到的是减少与实现类的耦合,提高可维护性。
二、带着上面的问题,在 TextView 类中找到下面的代码,提供了更换 Factory 的可能,使得开发者方便做不同的控件实现。
三、那么如何自定义控件的实现呢,需要新写一个接口继承 Editable, 并且覆盖掉 Editable 中下面的方法:
四、TextView.BufferType.EDITABLE 和 TextView.BufferType.SPANNABLE
有什么区别呢?
我们透过UML瞧一下,
Editable 类似于StringBuilder可追加字符,也就是说getText后可调用append方法设置文本内容。
Spannable 则可在给定的字符区域使用样式。
有意思的是 Editable 继承了 Spannable 所以具备较多的功能。
http://litonggang-gmail-com.iteye.com/upload/picture/pic/48117/f31ec7f1-00e9-31ca-a887-f304f4d8be9d.jpgTextView.BufferType枚举类型" />
常常使用setText(CharSequence text)为TextView赋值。
但是你有没有注意到,textview还有个方法:
public void
注意到:
TextView 控件设置文本时的参数有TextView.BufferType.EDITABLE 和 TextView.BufferType.SPANNABLE
但是不知道具体啥差别, 于是决定窥探 Google 代码,看个究竟。
一、在 TextView 类中找到 setText 方法关于 BufferType 的主要代码:
Java代码 http://litonggang.iteye.com/images/icon_star.pngTextView.BufferType枚举类型" />
-
//
Editable 是一个接口,作者巧妙的在接口中实现了工厂方法。 -
private
Editable.Factory mEditableFactory = Editable.Factory.getInstance(); -
private
Spannable.Factory mSpannableFactory = Spannable.Factory.getInstance();
-
if
(type null== BufferType.EDITABLE || mInput != || -
needEditableForNotificat ion) { -
Editable t = mEditableFactory.newEditable(text); -
text = t; -
setFilters(t, mFilters); -
InputMethodManager imm = InputMethodManager.peekInstance(); -
if (imm null)!= imm.restartInput( this); -
} else if (type null)== BufferType.SPANNABLE || mMovement != { -
text = mSpannableFactory.newSpannable(text); -
} else if (!(text instanceofCharWrapper)) { -
text = TextUtils.stringOrSpannedString(text); -
}
可以看出根据不同 BufferType ,用类工厂的模式创建控件,那么为什么不直接 new 一个控件呢?首先想到的是减少与实现类的耦合,提高可维护性。
二、带着上面的问题,在 TextView 类中找到下面的代码,提供了更换 Factory 的可能,使得开发者方便做不同的控件实现。
Java代码 http://litonggang.iteye.com/images/icon_star.pngTextView.BufferType枚举类型" />
-
public
final void setEditableFactory(Editable.Factory factory) { -
mEditableFactory = factory; -
setText(mText); -
} -
public final void setSpannableFactory(Spannable.Factory factory) { -
mSpannableFactory = factory; -
setText(mText); -
}
三、那么如何自定义控件的实现呢,需要新写一个接口继承 Editable, 并且覆盖掉 Editable 中下面的方法:
Java代码 http://litonggang.iteye.com/images/icon_star.pngTextView.BufferType枚举类型" />
-
public
static class Factory { -
private static Editable.Factory newsInstance = Editable.Factory(); -
public
static Editable.Factory getInstance() { -
return sInstance; -
} -
public
Editable newEditable(CharSequence source) { -
return new SpannableStringBuilder(source); -
} -
}
四、TextView.BufferType.EDITABLE 和 TextView.BufferType.SPANNABLE
有什么区别呢?
我们透过UML瞧一下,
Editable 类似于StringBuilder可追加字符,也就是说getText后可调用append方法设置文本内容。
Spannable 则可在给定的字符区域使用样式。
有意思的是 Editable 继承了 Spannable 所以具备较多的功能。
http://litonggang-gmail-com.iteye.com/upload/picture/pic/48117/f31ec7f1-00e9-31ca-a887-f304f4d8be9d.jpgTextView.BufferType枚举类型" />