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变成只接受电话号码输入的文本框,连软键盘都已经变成拨号专用软键盘了,所以不用再担心输入其他字符了
<?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变成只接受电话号码输入的文本框,连软键盘都已经变成拨号专用软键盘了,所以不用再担心输入其他字符了
相关文章推荐
- android.widget.edittext在4.0为何不显示光标?2.3 正常。
- 安卓出现错误: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
- java.net.MalformedURLException: Protocol not found: android.widget.EditText
- Graphical Layout出现EditText (Change to android.widget.EditText, Fix Build Path, Edit XML)
- java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
- The following classes could not be found - EditText (Change to android.widget.EditText, Fix Build Pa
- java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
- Android Studio 自动找不到android.widget.EditText
- android edittext点击图片显示隐藏密码
- Android 让EditText输入框失去焦点
- Android之如何改变焦点状态【EditText】
- android EditText控制最大输入行数
- Android中EditText限制只能输入 电话号码 字母 邮箱地址
- Android的EditText不弹出软键盘的方法
- Android EditText全选
- android edittext 自动不获取焦点
- Android EditText输入的一些限制
- Android Edittext Inputtype
- Android中不让EditText自动获取焦点
- android EditText中inputType的属性列表