类结构图:

说明:EditText是一种可编辑输入的控件,,由类结构图可以看到它是TextView的子类。所以它有TextView的一些属性,下面就是一个EditText的样例

实战演练:
1、如何设置最多输入N个字符
通过:android:maxLength来设置
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="提示文字" android:maxLength="4" />
效果:只能输入4个字

2、如何设置只能输入数字?
通过android:numeric其值有:integer signed decimal
<EditText android:id="@+id/myETxt02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:numeric="integer" />
3、如何设置成密码输入形式?
通过设置android:password="true"就可以
<EditText android:id="@+id/myETxt02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:password="true" />
效果:

4、如何设置成不可编辑状态
方式1:android:editable="false"
<EditText android:id="@+id/myETxt04" android:layout_width="fill_parent" android:layout_height="wrap_content" android:editable="false" android:text="不可编辑状态" />
方式2:
myEditText05 = (EditText) findViewById(R.id.myETxt05); // 设置成不可编辑状态 myEditText05.setEnabled(false);
这样就相当于TextView 控件一样
本文详细介绍Android中EditText控件的使用方法,包括限制输入长度、设置输入类型为数字、设置密码输入样式以及设置不可编辑状态等实用技巧。
1124

被折叠的 条评论
为什么被折叠?



