Android Design Support Library中提供了这个布局文件
使用该布局 需要在build.gradle中的dependencies块中添加两个依赖来向下兼容
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.1.0'
}
我在我的Lgin页面运用了TextInputLayoutcontent-login.xml
<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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.alan.blogs.LoginActivity"
tools:showIn="@layout/activity_login"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/email_TextInputLayout"
android:layout_marginTop="32dp"
android:focusable="true"
android:focusableInTouchMode="true"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="@string/emailHint"
android:ems="10"
android:id="@+id/edit_email" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/password_TextInputLayout">
<EditText
android:inputType="textPassword"
android:id="@+id/edit_password"
android:hint="@string/passwordHint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</android.support.design.widget.TextInputLayout>
<Button
android:layout_marginTop="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/login"
android:textColor="@color/white"
android:background="@xml/ripple"
/>
</LinearLayout>
在xml中声明android.support.design.widget.TextInputLayout然后再这个布局中添加且只能添加一个editText控件
属性都是正常填
在edittext中设置Hint
Textinputlayout会自动获取到这个Hint
public void init(){
this.emailInputLayout=(TextInputLayout)findViewById(R.id.email_TextInputLayout);
this.passwordInputLayout=(TextInputLayout)findViewById(R.id.password_TextInputLayout);
final EditText emailEditText=emailInputLayout.getEditText();
EditText passwordEditText=passwordInputLayout.getEditText();
// emailInputLayout.setHint("请输入Email");
// passwordInputLayout.setHint("输入密码"); //也可以在editText中设置Hint InputLayput会自动获取该Hint
emailEditText.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(emailEditText.getText().toString().contains("@")==false){
emailInputLayout.setErrorEnabled(true); emailInputLayout.setError("请输入正确的Email地址");
}else {
emailInputLayout.setErrorEnabled(false);
}
}
});
}
获取到TextinputLayout对象后。可以通过该对象的 getEditdText()方法获取到该布局中的子控件TextinputLayout还有一个好的功能就是可以为输入框添加一些 提示
比如emailTextIputLayout中需要检查是否包含“@”字符
方法: 为emailTextIputLayout添加 addTextChangedListener(new TextWatcher())
会有三个回调函数。功能就是在输入之前或之后或者正在输入时 可以添加你想要写的逻辑代码
在这三个回调函数中检查输入的字符串是否正确
要想让TextinputLayout提示错误信息
首先要设置TextinputLayout将这个功能打开
emailInputLayout.setErrorEnabled(true);然后输出错误信息
emailInputLayout.setError("请输入正确的Email地址");
当检测到用户输入正确时 使用emailInputLayout.setErrorEnabled(false);来关闭该功能