使用TextInputLayout实现不一样的登录界面

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_username;//用户名
private EditText et_password;//用户密码
private Button mLogin;//登录
private TextInputLayout tl_username;
private TextInputLayout tl_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tl_password = (TextInputLayout) findViewById(R.id.tl_password);
    tl_username = (TextInputLayout) findViewById(R.id.tl_username);
    et_password = (EditText) findViewById(R.id.et_password);
    et_username = (EditText) findViewById(R.id.et_username);
    mLogin = (Button) findViewById(R.id.bt_button);
    mLogin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.bt_button:
            login();
            break;
    }
}
//验证密码不能小于六位
private boolean validatePassword(String password) {
    return password.length() > 6;
}
//验证用户名为邮箱,使用正则表达式来验证
private static String EMALL_PATTERN = "^([a-z0-9A-Z]+[-|\\\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\\\.)+[a-zA-Z]{2,}$";
private Pattern pattern = Pattern.compile(EMALL_PATTERN);
//验证用户名
private boolean validateUsername(String username) {
    //return username.length()>6;
    return pattern.matcher(username).matches();
}
//用户登录
private void login() {
    String username = tl_username.getEditText.getText().toString().trim();
    String password = tl_password.getEditText.getText().toString().trim();
    //判断用户名和密码是否为空
    if (TextUtils.isEmpty(username)) {
        Toast.makeText(getApplicationContext(), "用户名不能为空", Toast.LENGTH_LONG).show();
        return;
    }
    if (TextUtils.isEmpty(password)) {
        Toast.makeText(getApplicationContext(), "密码不能为空", Toast.LENGTH_LONG).show();
        return;
    }
    //验证用户名和密码是否合法
    if (!validatePassword(password)) {
        //如果用密码不够六位
        tl_password.setErrorEnabled(true);
        tl_password.setError("密码位数不够");
    } else if (!validateUsername(username)) {
        tl_username.setErrorEnabled(true);
        tl_username.setError("请正确的输入邮箱地址");
    } else {
        tl_username.setErrorEnabled(false);
        tl_password.setErrorEnabled(false);
        Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show();
    }
}

}

布局文件

   
    
  

  
    
  
  

你可能感兴趣的:(使用TextInputLayout实现不一样的登录界面)