Android 自学之基本界面组件(上)
文本款(TextView)和编辑框(EditText)的功能和用法
TextView直接继承了View,他还是EditText、Button两个UI组件的父类,TextView的作用就是在界面上显示文字(相当于Java中AWT中标签[JLabel],但有比他强大些)。
TextView类及其子类的类图如图所示:

TextView的XML属性及相关方法和说明:
| XML属性 | 相关方法 | 说明 |
| android:autoLink | setAutoLinkMask(int) | 是否符合指定格式的文本转换为可单击的超链接形式 |
| android:cursorVisible | setCursorVisible(boolean) | 设置该文本框的光标是否可见 |
| android:drawableBottom |
setCompounDrawablesWithIntrinsicBounds (Drawable,Drawable,Drawable,Drawable) |
在文本框内文本的底端绘制指定图像 |
| android:drawableLeft |
setCompounDrawablesWithIntrinsicBounds (Drawable,Drawable,Drawable,Drawable) |
在文本框内文本的左边绘制指定图像 |
| android:drawablePadding |
setCompounDrawablesWithIntrinsicBounds (Drawable,Drawable,Drawable,Drawable) |
设置文本框内文本与图形之间的间距 |
| android:drawableRight |
setCompounDrawablesWithIntrinsicBounds (Drawable,Drawable,Drawable,Drawable) |
在文本框内文本的右边绘制指定图像 |
| android:drawableTop |
setCompounDrawablesWithIntrinsicBounds (Drawable,Drawable,Drawable,Drawable) |
在文本框内文本的顶端绘制指定图像 |
| android:editable | 设置该文本是否允许编辑 | |
| android:ellipsize | 设置当显示的文本超过TextView的长度是如何处理文本内容 | |
| android:gracity | setGravity(int) | 设置文本框内文本的对齐方式 |
| android:height | setHeight(int) | 设置该文本框的高度(以pixel为单位) |
| android:hint | setHint(int) | 设置当该文本框内容为空时,文本框内默认显示的提示文本 |
| android:minHeight | setMinHeight(int) | 设置该文本框的最小高度(以pixel为单位) |
| android:maxHeight | setMaxHeight(int) | 设置该文本框的最大高度(以pixel为单位) |
| android:minWidth | setMinWidth(int) | 设置该文本框的最小宽度(以pixel为单位) |
| android:maxWidth | setMaxWidth(int) | 设置该文本框的最大宽度(以pixel为单位) |
| android:lines | setLines(int) | 设置该文本框默认占几行 |
| android:MiLines | setMiLines(int) | 设置该文本框最少占几行 |
| android:MaxLines | setMaxLines(int) | 设置该文本框最多占几行 |
| android:password | setTransformationMethod(TransfirmationMethod) | 设置该文本框是一个密码框(以点代替字符) |
| android:phoneNumber | setKevListener(KeyListener) | 设置该文本框只能接受电话号码 |
| android:scrollHorizontally | setHorizontallyScorlling(boolean) | 设置当该文本框不够显示全部内容时是否允许水平滚动 |
| android:selectAllOnFocus | setSelectAllOnFocus(boolean) | 如果文本框的内容可选择,设置当它获得焦点时是否自动选中所有文本 |
| android:singleLine | setTransformationMethod | 设置该文本框是否为单行模式。如果设为true,文本框不会换行 |
| 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:tetColorHighlight | setHighlightColor(int) | 设置该文本框内文本被选中时的颜色 |
| android:textScaleX | setTextScaleX(float) | 设置该文本框内文本水平方向上的缩放因子 |
| android:textSize | setTextSize(float) | 设置该文本框内文本的字体大小 |
| android:textStyle | setTypeface(Typeface) | 设置该文本框内文本的字体风格,如粗体,斜体 |
| android:typeface | setTypeface(Typeface) | 设置该文本框内文本的字体 |
| android:width | setWidth(int) | 设置该文本框的宽度 |
上表中android:autoLink属性值是如下几个属性值的一个或几个,多个属性值之间用竖线隔开:
- none:不设置任何超链接
- web(对应于Linkify.WEB_URLS):将文本中的URL地址转换为超链接
- email(对应于Linkify.EMAIL_ADDRESSES):将文本中的E-mail地址转换为超链接
- phone(对应于Linkify.PHONE_NUMBERS):将文本中的电话号码转换为超练接
- map(对应于Linkify.MAP_ADDRESSES):将文本中的接到地址转换为超链接
- all:相当于指定weblemaillphonelmap
上表中android:ellipsize属性可支持如下几个属性值。
- none:不进行任何处理
- start:在文本开头部分进行省略
- middle:在文本中间部分进行省略
- end:在文本结尾出进行省略
- marquee:在文本结尾出以淡出的方式省略
下面我们用例子来例举上表中的一些属性:不同字体、不同颜色的文本、URL
layout/main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <!-- 设置字体为20pt -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我爱Java"
android:textSize="20pt"
/>
<!-- 设置中间省略 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="我爱Java我爱Java我爱Java我爱Java我爱Java我aaaJava"
android:ellipsize="middle"
/>
<!-- 对邮件增加链接 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="测试kongyeeku@163.com内容"
android:autoLink="email"
/>
<!-- 设置文字颜色 、大小,并使用阴影 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="测试文字"
android:shadowColor="#0000ff"
android:shadowDx="15.0"
android:shadowDy="20.0"
android:shadowRadius="45.0"
android:textColor="#ff0000"
android:textSize="25pt"
/>
<!-- 测试密码框 -->
<TextView android:id="@+id/passwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:password="true"
/> </LinearLayout>
所展现的效果图如下:

范例:带边框、图片的TextView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <!-- 通过android:background指定背景 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="带边框的文本"
android:background="@drawable/bg_border"
android:textColor="@color/abc_search_url_text_holo"
/> <!-- 通过android:drawableLeft绘制一张图片 -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="带图片的文本"
android:drawableLeft="@drawable/ic_launcher"
/> </LinearLayout>
效果图:

范例:一个注册界面
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="10sp"
android:background="@drawable/bg_border"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请填写登录帐号"
android:selectAllOnFocus="true"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="10pt"
android:background="@drawable/bg_border"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"
/>
</TableRow>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="电话号码:"
android:textSize="10pt"
android:background="@drawable/bg_border"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请填写您的电话号码"
android:selectAllOnFocus="true"
android:phoneNumber="true"
/>
</TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注册"
/> </TableLayout>
上面代码里面的TextView是设置了背景图片的,所以在展示的效果图上你们呢会看到三个黑的框框,为什么三个框框的会有一个比另外两个小,那是因为我设置的宽度单位不一样(用户名的宽度是sp,其他的是用的pt);所展示的效果,效果图如下:

Android 自学之基本界面组件(上)的更多相关文章
- Android 自学之基本界面组件(下)
按钮(Button)与图片按钮(ImageButton)组件的功能和用法 Button继承了TextView,ImageButton继承了Button.不管是Button还是ImageButton,他 ...
- Android学习使用基本界面组件(下拉框,单选框,复选框,数字转轮,滚动条)
(一)建立单选框按钮 RadioGroup和RadioButton建立单选框按钮 字符串资源文件: <resources> <string name="app_name&q ...
- Android中如何区分界面组件创建和销毁的类型
本文主要描述: 1.分辨系统杀掉退出还是用户主动退出2.分辨全新的创建还是系统恢复性的创建 1.分辨系统杀掉退出还是用户主动退出 当一个组件失去焦点后,系统有可能为了释放资源而杀掉这个组件,这个时候系 ...
- Android界面组件的四种启动方式
Android界面组件启动有四种方式 standard,singleTop,singleTask,singleInstance. standard:每次调用都会都会产生新的组件. singletop: ...
- Android 自学之画廊视图(Gallery)功能和用法
Gallery与之前讲的Spinner有共同的父类:AbsSpinner,表明Gallery和Spinner都是一个列表框.他们之间的区别在于Spinner显示的是一个垂直的列表框,而Gallery显 ...
- Android 自学之进度条ProgressBar
进度条(ProgressBar)也是UI界面中的一种非常使用的组件,通常用于向用户显示某个耗时完成的百分比.因此进度条可以动态的显示进度,因此避免长时间地执行某个耗时操作时,让用户感觉程序失去了响应, ...
- Android 自学之帧布局 FrameLayout
帧布局(FrameLayout)直接继承了ViewGroup组件: 帧布局容器为每一个加入其中的组件都创建了一个空白的区域,这个区域我们称之为一帧,所有每个组件都占据一帧,这些都会根据gravity属 ...
- 收藏的Android很好用的组件或者框架。
收藏的Android很好用的组件或者框架. android框架 先说两个站点: http://www.androidviews.net/ 非常好的国外开源码站,就是訪问速度有点慢啊 http://w ...
- Android 开发:由模块化到组件化(一)
在Android SDK一文中,我们谈到模块化和组件化,现在我们来聊聊组件化开发背后的哪些事.最早是在广告SDK中应用组件化,但是同样适用于普通应用开发 以下高能,请做好心理准备,看不懂请发私信来交流 ...
随机推荐
- selenium IDE & Remote Control & Webdriver
一直忘记写selenium的开始学习的过程,今天趁五一,天气有雨,写下这文章 1.进入selnium官网,了解selenium1,2,grid的区别.下载c#相关的包(使用c#的人非常少) 2.使用I ...
- CodeForce---Educational Codeforces Round 3 The best Gift 解题报告
对于这题笔者认为可以用数学排列来算,但是由于笔者很懒所以抄了一段大神的代码来交个大家了, 这位大神的基本想法就是通过记录各类书的数量,再暴力破解: 下面贴出这位大神的代码吧: #include< ...
- 【译】 AWK教程指南 4通过文本内容和对比选择指定的记录
Pattern { Action }为awk中最主要的语法.若某Pattern的值为真则执行它后面的 Action. awk中常使用"关系表达式" (Relational Expr ...
- IO_REMOVE_LOCK使用方法小结(转载加改正)
原文链接:http://www.programlife.net/io_remove_lock.html IO_REMOVE_LOCK(删除锁)的具体结构没有公开,WDK的文档中中查不到IO_REMOV ...
- 【转】 bash简介及通配符、扩展通配符 shopt -s extglob
http://www.rhce.cc/?p=1005 当我们执行一些命令的时候,很多的命令是由bash提供的.如果我们想知道某个命令是否是由bash内置的命令的话,我们可以使用type bash内置命 ...
- 【转】linux trap
在有些情况下,我们不希望自己的shell脚本在运行时刻被中断,比如说我们写得shell脚 本设为某一用户的默认shell,使这一用户进入系统后只能作某一项工作,如数据库备份, 我 们可不希望用户使用c ...
- HW6.20
public class Solution { public static void main(String[] args) { int[][] chessboard = new int[8][8]; ...
- [C++]VisualAssistX中文注释提示错误 解决办法
问题情况:Visual Assist X中文注释为提醒注释错误,而且在注释下面以红线标.问题原因:这是因为Visual Assist X认为中文的注释是拼写错误.问题处理: 1.打开 Microsof ...
- CALayer精讲
前言 CALayer包含在QuartzCore框架中,这是一个跨平台的框架,既可以用在iOS中又可以用在Mac OS X中.后面要学Core Animation就应该先学好Layer(层). 我们看一 ...
- 一个通用数据库访问类(C#,SqlClient)
本文转自:http://www.7139.com/jsxy/cxsj/c/200607/114291.html使用ADO.NET时,每次数据库操作都要设置connection属性.建立connecti ...