您的位置:首页 > 移动开发 > Android开发

Android Design TextinputLayout

2016-03-03 15:59 344 查看


使用该布局 需要在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'
}


<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>


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);
}
}
});
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: