高级炫酷带有提示的输入框,相当于输入框中的战斗框
使用
依赖design类库
compile 'com.android.support:design:23.0.0+'
布局
包裹一个TextInputEditText,并且一个TextInputLayout只能包裹一个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="25dp">
<android.support.design.widget.TextInputLayout
android:id="@+id/tv_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:id="@+id/tv_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:textColor="@color/colorAccent"
android:textColorHint="@color/colorAccent" />
</android.support.design.widget.TextInputLayout>
<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="请输入密码"
android:textColor="@color/colorAccent"
android:textColorHint="@color/colorAccent" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.FloatingActionButton xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fzb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="@drawable/ic_search"
app:elevation="15dp"
app:fabSize="normal"
app:rippleColor="@color/colorPrimary" />
</LinearLayout>
监听输入框—设置错误提示信息
//监视输入框的输入
tvInput.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) {
}
//输入框个改变后
@Override
public void afterTextChanged(Editable s) {
if (s.length() > 5) {
tvInput.setError("错误不能超过5位");
}
}
});
先在TextInputLayout中包裹一个EditText,如:
//获取EditText
EditText editText = inputLayout.getEditText();
//设置错误提示信息
inputLayout.setError("不能超过5个");
//启用错误提示
inputLayout.setErrorEnabled(true);