EditText
2017-02-14 本文已影响29人
CP9
Input Type
类型 | 描述 |
---|---|
textUri | Text that will be used as a URI |
textEmailAddress | Text that will be used as an e-mail address |
textPersonName | Text that is the name of a person |
textPassword | Text that is a password that should be obscured |
number | A numeric only field |
phone | For entering a phone number |
date | For entering a date |
time | For entering a time |
textMultiLine | Allow multiple lines of text in the field |
<EditText
android:inputType="textCapSentences|textMultiline"
/>
单行显示
<EditText
android:singleLine="true"
android:lines="1"
/>
限制输入字符
<EditText
android:inputType="number"
android:digits="01"
/>
限制字符总数
<EditText
android:maxLength="5"
/>
设置选定文本的高亮背景颜色
<EditText
android:textColorHighlight="#7cff88"
/>
edit-highlight-color.png
设置输入框底边颜色
- 导入AppCompat库
- 修改样式
colorControlNormal
、colorControlActivated
、colorControlHighlight
<style name="Theme.App.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">#d32f2f</item>
<item name="colorControlActivated">#ff5722</item>
<item name="colorControlHighlight">#f44336</item>
</style>
edit-bottom-color.png
显示浮动标签提示
- 导入design support library
- 使用
TextInputLayout
包裹EditText
:
<android.support.design.widget.TextInputLayout
android:id="@+id/username_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/etUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Username" />
</android.support.design.widget.TextInputLayout>
- 使用
setError
和setErrorEnabled
方法设置显示错误消息
private void setupFloatingLabelError() {
final TextInputLayout floatingUsernameLabel = (TextInputLayout) findViewById(R.id.username_text_input_layout);
floatingUsernameLabel.getEditText().addTextChangedListener(new TextWatcher() {
// ...
@Override
public void onTextChanged(CharSequence text, int start, int count, int after) {
if (text.length() > 0 && text.length() <= 4) {
floatingUsernameLabel.setError(getString(R.string.username_required));
floatingUsernameLabel.setErrorEnabled(true);
} else {
floatingUsernameLabel.setErrorEnabled(false);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
edit-float.gif
添加字符计数
TextInputLayout需要添加app:counterEnabled
和app:CounterMaxLength
属性,或者使用setCounterEnabled()
和setCounterMaxLength()
方法
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:counterEnabled="true"
app:counterMaxLength="10"
app:counterTextAppearance="@style/counterText"
app:counterOverflowTextAppearance="@style/counterOverride">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Username"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Username" />
</android.support.design.widget.TextInputLayout>
edit-character-counting.png
切换密码可见性
- passwordToggleEnabled 启用或禁用密码可见性的切换
- passwordToggleDrawable 更改默认的图标
- passwordToggleTint 更改默认的颜色
<android.support.design.widget.TextInputLayout
android:id="@+id/username_text_input_layout"
app:passwordToggleEnabled="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/etUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:inputType="textPassword"
android:hint="Username" />
</android.support.design.widget.TextInputLayout>
edit-password-toggle.png
自定义
提示文本可以通过定义app:hintTextAppearance
来设置样式,并且可以使用app:errorTextAppearance
更改错误文本。 通过定义app:counterTextAppearance
和app:counterOverflowTextAppearance
,计数器文本和溢出文本也可以有自己的文本样式。 我们可以使用textColor,textSize和fontFamily来帮助更改颜色,大小或字体:
<style name="counterText">
<item name="android:textColor">#aa5353cc</item>
</style>
<style name="counterOverride">
<item name="android:textColor">#ff0000</item>
</style>