普通的登录界面图片:
EditText 的hint带动画效果,如图:
TextInputLayout 控件是一个容器,它跟 ScrollView 一样只接受一个子元素,并且这个子元素是 EditText。
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:id="@+id/activity_text_input_layout"
tools:context=".MainActivity">
<android.support.design.widget.TextInputLayout
android:id="@+id/tl_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<EditText
android:id="@+id/edt_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:hint="用户名"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:maxLength="25"
android:maxLines="1"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="@+id/tl_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tl_username"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_marginTop="20dp">
<EditText
android:id="@+id/edt_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edt_username"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:hint="密码"
android:maxLength="25"
android:maxLines="1"/>
</android.support.design.widget.TextInputLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tl_password"
android:layout_centerHorizontal="true"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:text="登录"/>
</RelativeLayout>
显示错误信息,用来提示用户:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private TextInputLayout tl_username,tl_password;
private Matcher matcher;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tl_username = findViewById(R.id.tl_username);
tl_password = findViewById(R.id.tl_password);
button = findViewById(R.id.btn_login);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
login();
}
});
}
private void login() {
String username = tl_username.getEditText().getText().toString(); // 用户名
String password = tl_password.getEditText().getText().toString(); // 密码
if (!validateUsername(username)){
tl_username.setErrorEnabled(true);
tl_username.setError("账号不能超过3位");
}else if (!validatePassword(password)){
tl_password.setErrorEnabled(true);
tl_password.setError("密码不能少于3位");
}else{
tl_username.setErrorEnabled(false);
tl_password.setErrorEnabled(false);
Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_SHORT).show();
}
}
private boolean validatePassword(String password){
return password.length() > 3; // 字符长度不能小于3位
}
private boolean validateUsername(String username){
return username.length() < 3; // 字符长度不能大于3位
}
}