加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Android TextView.BufferType枚举类型

(2012-05-09 17:24:16)
标签:

android

textview

buffer

杂谈

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枚举类型" />
  1. // Editable 是一个接口,作者巧妙的在接口中实现了工厂方法。  
  2. private Editable.Factory mEditableFactory Editable.Factory.getInstance();  
  3. private Spannable.Factory mSpannableFactory Spannable.Factory.getInstance();  
Java代码  http://litonggang.iteye.com/images/icon_star.pngTextView.BufferType枚举类型" />
  1. if (type == BufferType.EDITABLE || mInput != null ||  
  2.             needEditableForNotification)  
  3.             Editable mEditableFactory.newEditable(text);  
  4.             text t;  
  5.             setFilters(t, mFilters);  
  6.             InputMethodManager imm InputMethodManager.peekInstance();  
  7.             if (imm != nullimm.restartInput(this);  
  8.         else if (type == BufferType.SPANNABLE || mMovement != null 
  9.             text mSpannableFactory.newSpannable(text);  
  10.         else if (!(text instanceof CharWrapper))  
  11.             text TextUtils.stringOrSpannedString(text);  
  12.          


可以看出根据不同 BufferType ,用类工厂的模式创建控件,那么为什么不直接 new 一个控件呢?首先想到的是减少与实现类的耦合,提高可维护性

二、带着上面的问题,在 TextView 类中找到下面的代码,提供了更换 Factory 的可能,使得开发者方便做不同的控件实现。
   
Java代码  http://litonggang.iteye.com/images/icon_star.pngTextView.BufferType枚举类型" />
  1. public final void setEditableFactory(Editable.Factory factory)  
  2.         mEditableFactory factory;  
  3.         setText(mText);  
  4.      
  5.     public final void setSpannableFactory(Spannable.Factory factory)  
  6.         mSpannableFactory factory;  
  7.         setText(mText);  
  8.      


三、那么如何自定义控件的实现呢,需要新写一个接口继承 Editable, 并且覆盖掉 Editable 中下面的方法:

Java代码  http://litonggang.iteye.com/images/icon_star.pngTextView.BufferType枚举类型" />
  1. public static class Factory  
  2.         private static Editable.Factory sInstance new Editable.Factory();  
  3. public static Editable.Factory getInstance()  
  4.             return sInstance;  
  5.          
  6. public Editable newEditable(CharSequence source)  
  7.             return new SpannableStringBuilder(source);  
  8.          
  9.      


四、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枚举类型" />


0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有