UI组件:TextView及其子类
TextView的功能与用法
TextView直接继承了View,它还是EditView,Button两个UI组件的父类。
TextView与EditView有很多相似之处,他们之间的最大区别是EditView允许用户编辑编辑文本内容,而TextView不允许。
TextView提供了大量的XML属性,这些属性不仅适用于TextView,TextView的子类也同时适用。
TextView的XML属性
下面列举了TextView一些常用的XML属性及相关方法的说明:
XML属性 相关方法 ?说明
android:autoLink setAutoLinkMask(int) 是否将符合指定格式的文本转化成可点击的链接的形式。
该属性支持如下属性值:
none:没有匹配模式(默认值)
web:匹配URL网址
email:匹配邮箱地址
phone:匹配电话号码
map:匹配映射地址
all:匹配所有的模式
android:drawableTop setCompoundDrawablesWithIntrinsicBounds
(Drawable,Drawable,Drawable,Drawable) 在文本框内文本的顶端绘制指定图像
android:drawableEnd setCompoundDrawablesRelativeWithIntrinsicBounds
(Drawable,Drawable,Drawable,Drawable) 在文本框内文本的结尾绘制指定图像
android:drawableLeft setCompoundDrawablesWithIntrinsicBounds
(Drawable,Drawable,Drawable,Drawable) 在文本框内文本的左边绘制指定图像
android:drawableRight setCompoundDrawablesWithIntrinsicBounds
(Drawable,Drawable,Drawable,Drawable) 在文本框内文本的右边绘制指定图像
android:drawablePadding setCompoundDrawablePadding(int) 设置文本框内文本与图像之间的间距
android:ellipsize setEllipsize(TextUtils.TruncateAt) 设置当显示的文本超过TextView的长度时如何处理文本内容。
该属性支持如下属性值:
none:不做任何处理
start:在文本开始时截断,并显示省略号
middle:在文本中间截断,并显示省略号
end:在文本结束时截断,并显示省略号
marquee:使用marquee滚动动画显示文本
android:fontFamily setTypeface(Typeface) 设置文本框内文本的字体
android:gravity setGravity(int) 设置文本框内文本的对齐方式
android:hint setHint(int) 设置当文本框为空时,把文本框默认显示的文本内容
android:lines setLines(int) 设置该文本框默认占几行
android:shadowColor setShadowLayer(float,float,float,int) 设置文本框阴影的颜色
android:shadowDx setShadowLayer(float,float,float,int) 设置文本框阴影在水平方向的偏移
android:shadowDy setShadowLayer(float,float,float,int) 设置文本框阴影在垂直方向的偏移
android:shadowRadius setShadowLayer(float,float,float,int) 设置文本框阴影的模糊程度。该值越大,阴影越模糊。
android:text setText(CharSequence) 设置文本框的文本内容
android:textColor setTextColor(ColorStateList) 设置文本框文本的颜色
android:inputType setRawInputType(int) 指定该文本框的类型。该属性有点像HTML中的input元素type属性。
CheckedTextView的功能与用法
TextView派生出了一个CheckedTextView,CheckTextView增加了一个checked状态。
通过setChecked(boolean)和isChecked()方法来改变、访问该组件的checked状态。
通过setCheckMarkDrawable()方法来设置它的勾选图标。
TextView和CheckedTextView的基本实例
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.textviewinfo.MainActivity">
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="20sp"
android:drawableRight="@drawable/picture"
android:text="文本结尾绘制一张图片"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="1"
android:ellipsize="end"
android:text="长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本长文本"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:autoLink="email|phone"
android:text="邮件是kongyeeku@163.com,电话是16839323939"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:shadowColor="#3F51B5"
android:shadowDx="10.0"
android:shadowDy="8.0"
android:shadowRadius="3.0"
android:text="带有阴影的文字"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:inputType="textPassword"
android:text="hello"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:checkMark="@drawable/ok"
android:text="可勾选的文本"/>
EditText的功能与用法
EditView继承自TextView,可以使用TextView的相关XML属性。
EditText组件最重要的属性是inputType,用来为EditText指定类型的输入组件。
EditText的实例
android:orientation="vertical"android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="聚焦时所有的文本被选择"
android:selectAllOnFocus="true"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="数字输入框"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="数字密码输入框"
android:inputType="numberPassword"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码输入框"
android:inputType="textPassword"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="日期输入框"
android:inputType="date"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="时间日期输入框"
android:inputType="datetime"/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="电话号码输入框"
android:inputType="phone"/>
Android:hint指定文本的提示信息。
当焦点定位到相应的输入框,系统切换到相应的输入法。如点击数字密码输入框,系统会切换到数字输入法。
Button的功能与用法
Button继承自TextView,它主要是在UI界面上生成一个按钮,可添加点击事件。
可为Button添加android:background属性为按钮添加背景颜色或图片。
可使用自定义的Drawable对象,可以使按钮背景颜色或者图片随用户动作动态改变。
Button的实例
下面设置了普通按钮和使用了Drawable对象的按钮。
android:orientation="vertical"android:layout_width="match_parent"
android:layout_height="match_parent">
|
|