您的位置:首页 > 其它

Andorid Material Design TextInputLayout和TextInputEditText

2016-08-28 21:40 232 查看
TextInputLayout继承自LinearLayout

api:https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html

Layout which wraps an EditText (or descendant) to show a floating label when the hint is hidden due to the user inputting text.

Also supports showing an error via setErrorEnabled(boolean) and setError(CharSequence), and a character counter via setCounterEnabled(boolean).

The TextInputEditText class is provided to be used as a child of this layout. Using TextInputEditText allows TextInputLayout greater control over the visual aspects of any text input.

被设计成用来包裹一个EditText (或者其子类)的布局 ,当它在用户输入、提示文本被隐藏的同时来显示一个浮动的标签 ,同时能够通过 setErrorEnabled(boolean)和setError(CharSequence) 来支持显示一个错误显示,同时可以通过setCounterEnabled(boolean)来显示一个字符计数器,

而TextInputEditText 类是就是被设计成用来作为这个布局包裹子的控件来使用,使用它会让TextInputLayout 更加完善得控制任何输入文本的视觉效果。

An example usage is as so:

下面是简单的事例用法

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/form_username"/>

</android.support.design.widget.TextInputLayout>


api:https://developer.android.com/reference/android/support/design/widget/TextInputEditText.html

TextInputEditText继承自AppCompatEditText

A special sub-class of EditText designed for use as a child of TextInputLayout.

Using this class allows us to display a hint in the IME when in ‘extract’ mode.

一个特别的EditText 替代类,是被设计成用于TextInputLayout包裹的子控件来使用的,使用它在全屏模式下来显示android:hint提示文字。

所以TextInputLayout最常用的还是包裹EditText 来使用。。。

TextInputLayout常用xml属性

counterEnabled

Whether the layout is laid out as if the character counter will be displayed.

计数器是否可用

counterMaxLength

The max length to display in the character counter.

计数器最大字符长度

errorEnabled

Whether the layout is laid out as if an error will be displayed.

错误信息是否可用

hintAnimationEnabled

Whether to animate hint state changes.

提示信息动画是否可用

hintEnabled

Whether the layout’s floating label functionality is enabled.

提示信息是否可用

hintTextAppearance

提示信息外观

passwordToggleContentDescription

Text to set as the content description for the password input visibility toggle.

设置当前是密码可见状态的描述文本

passwordToggleDrawable

Drawable to use as the password input visibility toggle icon.

密码可见状态切换按钮图标

passwordToggleEnabled

Whether the view will display a toggle when the EditText has a password.

密码可见状态切换按钮是否可用

hint

The hint to display in the floating label.

提示信息

xml

<android.support.design.widget.TextInputLayout
android:id="@+id/id_til"
android:layout_width="200dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:hintAnimationEnabled="false"
android:layout_height="wrap_content">
<EditText
android:id="@+id/id_et"
android:layout_width="200dp"
android:layout_height="wrap_content"
/>
</android.support.design.widget.TextInputLayout>


code

final TextInputLayout idtil = (TextInputLayout) findViewById(R.id.id_til);
idet.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

if (s.toString().equals("")){
idtil.setError("is empty");
}else{
idtil.setError(null);
}
}

@Override
public void afterTextChanged(Editable s) {

}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: