TextInputLayout 的使用

 
  
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:background="#0ff"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/til">
            android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

你可能感兴趣的:(android)