使用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();
    }
}

}

布局文件

   <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/et_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="username"
        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_marginTop="20dp"
    android:layout_centerInParent="true"
    android:layout_centerHorizontal="true"   >
    <EditText
        android:id="@+id/et_password"
        android:hint="password"
        android:maxLines="1"
        android:maxLength="25"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
  </android.support.design.widget.TextInputLayout>
  <Button
    android:id="@+id/bt_button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tl_password"
    android:text="登录"
    android:textSize="20sp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="34dp" />
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容