参阅:http://android.xsoftlab.net/reference/android/widget/TextView.html
public class
TextView
extends View
implements ViewTreeObserver.OnPreDrawListener
Added in API level 1
java.lang.Object
↳ android.widget.TextView
已知直接子类
AppCompatTextView
Button
CheckedTextView
Chronometer
DigitalClock
EditText
RowHeaderView
TextClock
【概述】
TextView用于向用户显示文本,并可选择允许编辑它。
TextView是一个完整的文本编辑器,但是基本类被配置为不允许编辑;其子类EditText可对文本进行编辑。
设置XML属性android:textIsSelectable=”true”或者调用setTextIsSelectable(true),可允许用户对TextView的文本内容进行复制/粘贴操作。
备注:
使用<TextView>来输入文本通常是一个错误,您应该使用<EditText>。EditText是TextView的一个子类,一些编辑支持由TextView提供,因此可以在TextView上设置一些与输入相关的属性。 但是,使用TextView和输入属性通常是剪切和粘贴错误。 要输入文本,您应该使用<EditText>。 这个检查也检查TextView的子类,如Button和CheckBox,因为它们有相同的问题:它们不应该与可编辑的属性一起使用。
【总结】
【嵌套类】
枚举TextView.BufferType API1
接口TextView.OnEditorActionListenerAPI3
类TextView.SavedStateAPI1
【XML属性】
android:autoLink
API 1
关联方法:setAutoLinkMask(int mask)
属性说明: 设置是否显示为超链接。(none/web/email/phone/map/all) 默认为none。
可以是以下常量之一:
none 不匹配。默认值。
web 匹配URL地址,单击使用浏览器打开
email匹配邮箱地址,单击发送邮件
phone匹配电话号码,单击拨打电话
map 匹配地图地址,单击使用google地图打开,支持英文
all 匹配上述四种格式
它对应的全局资源符号是autoLink。
当TextView的内容既包含汉字又包含网址。如要想成功实践超链接功能,就必须在汉字后添加空格或者换行,否则地址就是此TextView的所有内容。
HTML解析超链接:通过Spanned对象设置text,而通过HTML的formHTML来获取Spanned对象
textView.setText(Html.fromHtml("百度:<a href='http://www.baidu.com'>http://www.baidu.com</a>
<h1><i><font color='#000FFF'>h1号字 斜体 蓝色</font></i>"));
android:autoText
API 1
关联方法:setKeyListener(KeyListener input)
属性说明: 如果为true,指定此TextView有一个文本输入法,并自动纠正一些常见的拼写错误。默认false。
可以是一个布尔值(boolean),“true”或“false”。
可以引用一个资源(格式:@[package:]type:name)或者是包含这种类型值的主题属性(格式:?[package][type:]name)。
它对应的全局资源符号是autoText。
全局属性autoText在API 3被弃用,使用inputType代替。
Android:autoText不应使用在TextView,应使用在EditView
android:breakStrategy
API 23
关联方法:setBreakStrategy(int breakStrategy)
属性说明:打破策略(控制段落布局)。
可以是以下常量之一:
simple 0
high_quality 1
balanced 2段落长度平衡
它对应的全局资源符号是breakStrategy。
android:bufferType
API 1
关联方法:setText(CharSequence,TextView.BufferType)
属性说明:指定getText()方式取得的文本类别。确定getText()将返回的最小类型。
可以是以下常量之一:
normal 默认。
editable 类似于StringBuilder可追加字符,getText后可调用append方法设置文本内容。
spannable 则可在给定的字符区域使用样式
它对应的全局资源符号是bufferType。
有意思的是 Editable继承了Spannable所以具备较多的功能。
注意,EditText和LogTextBox总是返回Editable,即使你在这里指定的功能更强大。
android:capitalize
API 1
关联方法:setKeyListener(KeyListener input)
属性说明:设置英文字母大写类型。
可以是以下常量之一:
none 不转换。默认值。
sentences 每一个句子首字母大写
words 每一个单词首字母大写
characters 每一个英文字母都大写
它对应的全局资源符号是bufferType。
全局属性capitalize在API 3被弃用,使用inputType代替。
Android:capitalize不应使用在TextView,应使用在EditView
android:cursorVisible
API 1
关联方法:setCursorVisible(boolean visible)
属性说明:设定光标为显示/隐藏,默认显示。如果设置false,即使选中了也不显示光标栏。
可以是一个布尔值(boolean),“true”或“false”。
还可以引用一个资源(格式:@[package:]type:name)或者是包含这种类型值的主题属性(格式:?[package][type:]name)。
它对应的全局资源符号是cursorVisible。
Android:cursorVisible不应使用在TextView,应使用在EditView
设置EditText光标在右边,输入内容从右往左。Android:gravity=”right/end”
android:digits
API 1
关联方法:setKeyListener(KeyListener input)
属性说明:设置允许输入哪些字符。如”1234567890.+-*/% ()”、”0123456789xyzXYZ”。常用于密码验证。
可以是一个字符串值(string),使用“\\;”等转义字符,“\\n”或“\\uxxxx”Unicode字符。
还可以引用一个资源(格式:@[package:]type:name)或者是包含这种类型值的主题属性(格式:?[package][type:]name)。
它对应的全局资源符号是digits。
Android:digits不应使用在TextView,应使用在EditView
android:drawablePadding
API 1
关联方法:setCompoundDrawablePadding(int pad)
属性说明:设置文本框内文本与图像之间的间距,与drawableLeft、drawableRight、drawableTop、drawableBottom一起使用,可设置为负数,单独使用没有效果。
可以是一个带有单位的浮点型尺寸值(float),如:”14.5sp”。有效的单位包括:px(像素)、dp(密度无关的像素)、sp(基于引用字体的尺寸来缩放的像素)、in(英寸)、mm(毫米)。
可以引用一个资源(格式:@[package:]type:name)或者是包含这种类型值的主题属性(格式:?[package][type:]name)。
它对应的全局资源符号是drawablePadding。
android:drawableTop
API 1
关联方法:setCompoundDrawablesWithIntrinsicBounds(int left,int top,int right,int bottom)
属性说明:在文本框顶部绘制指定图像。
android:drawableBottom
API 1
关联方法:setCompoundDrawablesWithIntrinsicBounds(int left,int top,int right,int bottom)
属性说明:在文本框底部绘制指定图像。
android:drawableLeft
API 1
关联方法:setCompoundDrawablesWithIntrinsicBounds(int left,int top,int right,int bottom)
属性说明:在文本框左边绘制指定图像。
android:drawableRight
API 1
关联方法:setCompoundDrawablesWithIntrinsicBounds(int left,int top,int right,int bottom)
属性说明:在文本框右边绘制指定图像。
android:drawableStart
API 14
关联方法:setCompoundDrawablesRelativeWithIntrinsicBounds(int start,int top,int end,int bottom)
属性说明:在文本框开始处绘制指定图像。
android:drawableEnd
API 14
关联方法:setCompoundDrawablesRelativeWithIntrinsicBounds(int start,int top,int end,int bottom)
属性说明:在文本框结尾处绘制指定图像。
可以引用一个资源(格式:@[package:]type:name)或者是包含这种类型值的主题属性(格式:?[package][type:]name)。
可以是一个颜色值(color),格式”#rgb”、”#argb”、”#rrggbb”、”#aarrggbb”。
它对应的全局资源符号是drawableTop、drawableBottom、drawableLeft、drawableRight、drawableStart、drawableEnd。
setCompoundDrawables(Drawable left,Drawable top,Drawable right,Drawable bottom) API 1
setCompoundDrawablesRelative(Drawablestart,Drawable top,Drawableend,Drawable bottom) API 17
Drawable必须已经调用了setBounds(Rect)。
setCompoundDrawablesWithIntrinsicBounds(Drawable left,Drawable top,Drawable right,Drawable bottom) API 1
setCompoundDrawablesWithIntrinsicBounds(int left,int top,int right,int bottom) API 3
setCompoundDrawablesRelativeWithIntrinsicBounds(Drawablestart,Drawable top,Drawableend,Drawable bottom) API 17
setCompoundDrawablesRelativeWithIntrinsicBounds(intstart,int top,intend,int bottom) API 17