final TextInputLayout til= (TextInputLayout) findViewById(R.id.til); til.setHint("please input some words:"); EditText et=til.getEditText(); et.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){ til.setError("some Error"); til.setErrorEnabled(true); }else { til.setErrorEnabled(false); } } });
//xml
<android.support.design.widget.TextInputLayout android:background="#0ff" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/til"> <android.support.design.widget.TextInputEditText android:layout_width="match_parent" android:layout_height="wrap_content" /> </android.support.design.widget.TextInputLayout>