您的位置:首页 > 移动开发 > Android开发

android widget 之EditText

2011-09-27 20:19 351 查看
EditText 相当于很多程序中的TextField,是一个可以让用户输入文本的组件,是用户和程序之间用于传输数据的纽带,通过它用户可以把数据传送给Android程序。
<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/lable1"
android:text="This is a Label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#cc0000"
android:textColor="#ffffff"
android:textSize="16sp"
android:padding="10dip"/>
<EditText android:id= "@+id/plaintext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="10dip"
android:hint="请输入一个值"
android:textColorHint="#238745"
android:maxLength="5"/>
<EditText android:id="@+id/passwordtext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="10dip"
android:password="true"
android:hint="请输入密码"/>
<EditText android:id="@+id/numbertext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="10dip"
android:numeric="signed"
android:hint="请输入数字"/>
<EditText android:id="@+id/phonenumbertext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="10dip"
android:phoneNumber="true"
android:hint="请输入电话号码"/>
<EditText android:id="@+id/inputtypetext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="10dip"
android:inputType="datetime"
android:hint="请输入电话号码"/>
</LinearLayout>
显示效果如下:



EditText集成自TextView,所以拥有TextView的所有属性。另外,EditText还有属于自己的属性。android:hint是EditText的默认提示文字,一般用于提示用户输入。android:textColorHint是提示文字的颜色,如果不设置,就为默认提示颜色。android:maxLength表示最大输入长度。android:password="true"就可以实现这一密码输入功能,可以看到其输入效果如下:




android:numeric="signed" 表示输入数字的类型,可以是Integer,一共有三种分别为integer(正整数)、signed(带符号整数)和decimal(浮点数):



android:phoneNumber="true"用于专门输入电话号码的文本框也是大有用途,有了他我们对是否是电话号码的校验就容易的多了(因为字符是正确的,只要校验格式 ).通过设置android:phoneNumber="true"就可以把EditText变成只接受电话号码输入的文本框,连软键盘都已经变成拨号专用软键盘了,所以不用再担心输入其他字符了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: