常用功能:注意:(一个TextInputLayout里面只可以包含一个EditText)
1.当EditText获取到焦点的时候,提示文字(hint)移动到输入框的上方。当焦点失去,自动 移到
EditText上。
<android.support.design.widget.TextInputLayout android:id="@+id/textInputAccount" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入账号" android:textColorHint="#ff6400" /> </android.support.design.widget.TextInputLayout>
2.显示错误信息。比如当输入的长度超过限制时,提示error错误。
private TextInputLayout textInputLayout; textInputLayout = (TextInputLayout) findViewById(R.id.textInputAccount); EditText editText = textInputLayout.getEditText(); editText.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) { textInputLayout.setError("超过了限制"); textInputLayout.setErrorEnabled(true); } else { textInputLayout.setErrorEnabled(false); } } });
3.显示用户已输入的字数。比如常见的发布内容的时候,右下角的提示(10/20)
counterEnabled这个属性设置是否开启字数统计。当只设置true的时候显示当前输入的个数。
counterTextAppearance是定义统计数字(10/20)的颜色。
counterMaxLength是设置当前文本框可输入的最大长度。
counterOverflowTextAppearance是超过限制字数之后,统计数字(21/20)的颜色)。
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" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_text_input_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.administrator.androidmatrialdesignproject.TextInputLayoutActivity"> <android.support.design.widget.TextInputLayout android:id="@+id/textInputPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" app:counterEnabled="true" app:counterMaxLength="20" app:counterOverflowTextAppearance="@style/counterOverflow"> <EditText android:layout_width="match_parent" android:layout_height="match_parent" android:hint="请输入密码" android:textColorHint="#ff6400" android:textSize="12sp" /> </android.support.design.widget.TextInputLayout> </LinearLayout>
Activity:
public class TextInputLayoutActivity extends AppCompatActivity { private TextInputLayout textInputPasswd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_text_input_layout); textInputPasswd = (TextInputLayout) findViewById(R.id.textInputPassword); final EditText editTextPasswd = textInputPasswd.getEditText(); editTextPasswd.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() > 20) { textInputPasswd.setErrorEnabled(true); textInputPasswd.setError("您的输入有误"); } else { textInputPasswd.setErrorEnabled(false); } } }); } }