TextInputLayout是什么
TextInputLayout主要是作為EditText的容器耸序,從而為EditText生成一個浮動的Label铅檩,當(dāng)用戶點(diǎn)擊EditText的時候寂拆,EditText中的hint字符串會自動移到EditText的左上角。
TextInputLayout如何使用
●基本用法
xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:hint="請輸入用戶名"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:hint="請輸入郵箱"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
●設(shè)置最大字符數(shù)及錯誤提示
xml
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_marginTop="20dp"
app:counterEnabled="true" //設(shè)置為true才能顯字符數(shù)
app:counterMaxLength="5" //設(shè)置最大字符數(shù)為5
app:counterOverflowTextAppearance="@style/HintError" //設(shè)置超出字符數(shù)后提示文字的顏色俏竞,如果不設(shè)置默認(rèn)為@color/colorAccent的顏色
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:hint="請輸入用戶名"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
style文件(設(shè)置超出字符數(shù)的文字提示顏色為紅色)
<style name="HintError" parent="TextAppearance.AppCompat">
<item name="android:textColor">@color/colorRed</item>
</style>
●設(shè)置錯誤提示文字
xml
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_marginTop="20dp"
app:errorEnabled="true" //設(shè)置為true
android:id="@+id/textinputlayout_email"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:hint="請輸入郵箱"
android:id="@+id/et_email"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>
java代碼
editText_email=findViewById(R.id.et_email);
textInputLayout =findViewById(R.id.textinputlayout_email);
editText_email.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(!RegexUtils.isEmail(charSequence)){
textInputLayout.setError("郵箱格式錯誤");
textInputLayout.setErrorEnabled(true);
}else {
textInputLayout.setErrorEnabled(false);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
●設(shè)置密碼是否可見
xml
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_marginTop="20dp"
app:errorEnabled="true"
app:passwordToggleEnabled="true" //設(shè)置為true
android:id="@+id/textinputlayout_password"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:hint="請輸入密碼"
android:id="@+id/et_password"
android:inputType="textPassword"
android:layout_height="wrap_content" />
</android.support.design.widget.TextInputLayout>