上一节中我们学习了第一个 UI控件TextView(文本框),文中给出了很多实际开发中可能遇到的一些需求 的解决方法,应该会为你的开发带来便利,在本节中,我们来学习第二个很常用的控件EditText(输入框); 和TextView非常类似,最大的区别是:EditText可以接受用户输入!和前面一样,我们不一个个讲属性, 只讲实际应用,要扣属性可以自己查看API文档:API文档;那么开始本节内容!
1.设置默认提示文本
如下图,相信你对于这种用户登录的界面并不陌生,是吧,我们很多时候都用的这种界面
相比另外这种,下面这种又如何?
还不赖是吧,当然,不会在这里贴布局,这里只介绍默认提示文本的两个控制属性:
默认提示文本的两个属性如下:
android:hint="默认提示文本" android:textColorHint="#95A1AA"
前者设置提示的文本内容,后者设置提示文本的颜色!
2.获得焦点后全选组件内所有文本内容
当我们点击想当我们的输入框获得焦点后,不是将光标移动到文本的开始或者结尾;而是 获取到输入框中所有的文本内容的话!这个时候我们可以使用selectAllOnFocus属性
android:selectAllOnFocus="true"
比如下面的效果图: 第一个是设置了该属性的,第二个是没设置该属性的,设置为true的EditText获得焦点后 选中的是所有文本!
3.限制EditText输入类型
有时我们可能需要对输入的数据进行限制,比如输入电话号码的时候,你输入了一串字母,这 显然是不符合我们预期的,而限制输入类型可以通过inputType属性来实现!
比如限制只能为电话号码,密码(textPassword):
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="phone" />
可选参数如下:
文本类型,多为大写、小写和数字符号
android:inputType="none" android:inputType="text" android:inputType="textCapCharacters" android:inputType="textCapWords" android:inputType="textCapSentences" android:inputType="textAutoCorrect" android:inputType="textAutoComplete" android:inputType="textMultiLine" android:inputType="textImeMultiLine" android:inputType="textNoSuggestions" android:inputType="textUri" android:inputType="textEmailAddress" android:inputType="textEmailSubject" android:inputType="textShortMessage" android:inputType="textLongMessage" android:inputType="textPersonName" android:inputType="textPostalAddress" android:inputType="textPassword" android:inputType="textVisiblePassword" android:inputType="textWebEditText" android:inputType="textFilter" android:inputType="textPhonetic"
数值类型
android:inputType="number" android:inputType="numberSigned" android