android-UI组件实例大全(二)------EditText编辑框
2015-12-04 19:00
513 查看
EditText与前面第一个UI组件TextView非常相似,与Textview的最大区别在于:前者可以接受用户输入
好的话不多说,上实例:
代码:
[html] view
plaincopyprint?
<span style="font-family:Comic Sans MS"> <!-- 为editText添加默认提示文本 -->
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="10pt" />
<EditText
android:id="@+id/editname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:selectAllOnFocus="true" />
</TableRow></span>
运行截图:
代码解释:
hint:制定了文本框提示信息的内容
selectAllOnFocus:若文本时可以编辑的,当该UI组件获得焦点以后不是将光标移到文本的开始或者结尾位置,而是获取组件内所有文本的内容
效果如下:
2.将editText设为密码框:
代码:
[html] view
plaincopyprint?
<span style="font-family:Comic Sans MS"><!-- 为editText设置输入限制,这里是设为密码框 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:textSize="10pt" />
<EditText
android:id="@+id/editpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" /></span>
运行截图:
代码解释:
inputType:指定文本框的类型,这里指定为密码框,输入后会变成...,当然也可以设为:
number:只接受数字输入
date:日期选输入框
phone:电话号码等
详细见一下链接:
http://blog.csdn.net/coder_pig/article/details/15505691
3.设置一个最少有2行,最多3行,且限制只能输入字母的,且文字间隔为1.5f
代码:
[html] view
plaincopyprint?
<!-- 我们设置一个最少有2行,最多3行,且限制只能输入字母的,且文字间隔为1.5f -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="备注"
android:textSize="10pt" />
<EditText
android:id="@+id/editpage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:capitalize="words"
android:textScaleX="1.5"
android:minLines="2"
android:maxLines="3"
/>
运行截图:
代码解释:
capitalize = "words"这里的话设置只能输入字母,且首字母会大写
textScaleX = 设置字与字之间的水平间距,默认1.0f
minLines:设置未输入东西时的显示行数
maxLines:设置编辑框的最大行数,超过以后向下滚动;也可使用android:lines设置,效果一样
4.控制组件四周的间隔距离与内部文字与边间的距离
代码:
[html] view
plaincopyprint?
<!-- 我们设置一个最少有2行,最多3行,且限制只能输入字母的,且文字间隔为1.5f -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="作为对比的EditText"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置padding = 20dp"
android:padding="20dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="设置margin = 20dp"
android:layout_margin="20dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="只设置左右的margin = 30dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
/>
运行截图:
代码解释:
我们使用margin相关属性增加组件与外框的距离
使用padding增加组件内文字和组件边框的距离
好的话不多说,上实例:
EditText选择框:
1.为我们的edit设置一个默认的提示文本:代码:
[html] view
plaincopyprint?
<span style="font-family:Comic Sans MS"> <!-- 为editText添加默认提示文本 -->
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="10pt" />
<EditText
android:id="@+id/editname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:selectAllOnFocus="true" />
</TableRow></span>
运行截图:
代码解释:
hint:制定了文本框提示信息的内容
selectAllOnFocus:若文本时可以编辑的,当该UI组件获得焦点以后不是将光标移到文本的开始或者结尾位置,而是获取组件内所有文本的内容
效果如下:
2.将editText设为密码框:
代码:
[html] view
plaincopyprint?
<span style="font-family:Comic Sans MS"><!-- 为editText设置输入限制,这里是设为密码框 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:textSize="10pt" />
<EditText
android:id="@+id/editpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" /></span>
运行截图:
代码解释:
inputType:指定文本框的类型,这里指定为密码框,输入后会变成...,当然也可以设为:
number:只接受数字输入
date:日期选输入框
phone:电话号码等
详细见一下链接:
http://blog.csdn.net/coder_pig/article/details/15505691
3.设置一个最少有2行,最多3行,且限制只能输入字母的,且文字间隔为1.5f
代码:
[html] view
plaincopyprint?
<!-- 我们设置一个最少有2行,最多3行,且限制只能输入字母的,且文字间隔为1.5f -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="备注"
android:textSize="10pt" />
<EditText
android:id="@+id/editpage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:capitalize="words"
android:textScaleX="1.5"
android:minLines="2"
android:maxLines="3"
/>
运行截图:
代码解释:
capitalize = "words"这里的话设置只能输入字母,且首字母会大写
textScaleX = 设置字与字之间的水平间距,默认1.0f
minLines:设置未输入东西时的显示行数
maxLines:设置编辑框的最大行数,超过以后向下滚动;也可使用android:lines设置,效果一样
4.控制组件四周的间隔距离与内部文字与边间的距离
代码:
[html] view
plaincopyprint?
<!-- 我们设置一个最少有2行,最多3行,且限制只能输入字母的,且文字间隔为1.5f -->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="作为对比的EditText"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置padding = 20dp"
android:padding="20dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="设置margin = 20dp"
android:layout_margin="20dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="只设置左右的margin = 30dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
/>
运行截图:
代码解释:
我们使用margin相关属性增加组件与外框的距离
使用padding增加组件内文字和组件边框的距离
相关文章推荐
- android UI组件属性------inputType
- android-UI组件实例大全(一)------TextView文本框
- JDBC的autoCommit为true时,其事务管理测试
- 产生制定位数的随机字符串,以及StringBuffer,StringBuilder区别
- hdu1159 Common Subsequence
- 4.ROS在Pcduino上的安装
- 数据库主键设计-用GUID还是Int(int32,int64)做主键(primaryKey)
- 对xib构建的UITableViewCell的重用方法和代码添加约束的方法 学习笔记
- hdu 5493 Queue treap实现将元素快速插入到第i个位置
- UI第十七天:图⽚异步下载、KVO
- remoteSort =true 用在store; 和sortable:true 用在column里的一个字段上
- iOS 新特性分列式 之 iOS 6.x - 主要内容:自动布局、集合视图、UI 状态保持
- 在iOS 8中使用UIAlertController
- HDUOJ Number Sequence找规律
- 杭电-5504GT and sequence
- poj 3239 Solution to the n Queens Puzzle n皇后问题的构造解法
- UILabel 字体下方加下划线
- EasyUI datagrid 获取全部数据
- NSData与UIImage之间的转换
- 设置UITextField键盘上return key不可点击