适用于EditText的Android Material Design浮动标签

时间:2016-06-16 12:23:52

标签: android material-design

我只是按照这个例子 http://www.androidhive.info/2015/09/android-material-design-floating-labels-for-edittext/ 假设我要验证确认密码..如何验证确认密码 这是验证密码

private boolean validatePassword() {
        if (inputPassword.getText().toString().trim().isEmpty()) {
            inputLayoutPassword.setError(getString(R.string.err_msg_password));
            requestFocus(inputPassword);
            return false;
        } else {
            inputLayoutPassword.setErrorEnabled(false);
        }

        return true;
    }

这是我的xml:

<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_password"
android:layout_width="match_parent"
android:layout_height="wrap_content">

        <EditText
            android:id="@+id/input_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_email" />

</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_password"
android:layout_width="match_parent"
android:layout_height="wrap_content">

        <EditText
            android:id="@+id/input_confirm_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_email" />

</android.support.design.widget.TextInputLayout>

2 个答案:

答案 0 :(得分:0)

确认密码与密码类似,只需要修改ID。

实施例

inputConfirmPassword = (TextInputLayout) findViewById(R.id.input_confirm_password);

private boolean validatePassword() {
    if (inputConfirmPassword.getText().toString().trim().isEmpty()) {
        inputConfirmPassword.setError(getString(R.string.err_msg_password));
        requestFocus(inputConfirmPassword);
        return false;
    } else {
        inputConfirmPassword.setErrorEnabled(false);
    }

    return true;
}

答案 1 :(得分:-1)

用户

public boolean isValid() {

    name = getEdit_name().getText().toString().trim();
    email = getEdit_email().getText().toString().trim();
    mobile = getEdit_mobile().getText().toString().trim();
    password = getEdit_password().getText().toString().trim();
    cnfrmPswrd = getEdit_cnfrm_pswrd().getText().toString().trim();

    String expLastname = "[a-zA-z]+([ '-][a-zA-Z]+)*";
    Pattern pattern = Pattern.compile(expLastname, Pattern.CASE_INSENSITIVE);
    Matcher matcher =pattern.matcher(name);

    if (name.equalsIgnoreCase("")) {
        showMessage("Enter Full Name");
        return false;
    }
    if(matcher.matches()){}
    else {
       showMessage("Please Enter Correct Full Name");
        return false;
    }
    if (email.equalsIgnoreCase("")) {
        showMessage("Enter Email");
        return false;
    }
    if (mobile.equalsIgnoreCase("")) {
        showMessage("Enter Mobile Number");
        return false;
    }
    if (TextUtils.isEmpty(mobile) || mobile.length() < 10) {
        showMessage("You Must have 10 Digits in Your Phone No.");
        return false;
    }
    if (!android.util.Patterns.EMAIL_ADDRESS.matcher(
            getEdit_email().getText().toString()).matches()) {
        showMessage("Invalid Email Address.");
        return false;
    }
    if (password.equalsIgnoreCase("")) {
        showMessage("Enter Password.");
        return false;
    }
    if (TextUtils.isEmpty(password) || password.length() < 4) {
        showMessage("Password Must be Between 4 to 20 Characters");
        return false;
    }
    if (!password.equals(cnfrmPswrd)) {
        showMessage("Password Not Match.");
        return false;
    } else
        return true;
}

按钮上的代码

if(isValid){
//do what you want }

showMessage的代码

public void showMessage(String msg) {
    Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}