android - EditText 的验证不起作用

标签 android android-layout android-activity android-studio android-edittext

我有一个注册屏幕。它包含用户全名、电子邮件地址和密码的位置(用户必须确认其密码)。

但是,它并没有像预期的那样工作。要启用注册按钮,必须按顺序填写字段:从上到下(这不是故意的)。

此外,确认密码字段上的红色不会消失,除非显示字符,否则注册按钮不会启用。检查是否所有字段中都有字符似乎出现故障,我不确定为什么。检查有时有效,有时无效。

请有人帮我优化这个。代码中有注释显示每个方法的作用。

这是布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"> 

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="@string/hintNameField"
        android:ems="10"
        android:id="@+id/txtSName"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp" />


    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:hint="@string/hintEmailField"
        android:ems="10"
        android:id="@+id/txtSEmailAddress"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp" />


    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/txtSPassword"
        android:hint="@string/hintPasswordField"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"/>

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/txtSPasswordConfirm"
        android:hint="@string/hintPasswordConfirmField"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/showPasswordCheckBox"
        android:id="@+id/showPasswordCheckBox"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="5dp"/>

    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="@string/btnSignUp"
        android:id="@+id/btnSignUp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp"
        android:enabled="false"
        />


</LinearLayout>

这是布局的 Java 类:

//this is the full name field
snameTxt = (EditText) findViewById(R.id.txtSName);
snameTxt.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) {

    }
});

snameTxt.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        Toast.makeText(getApplicationContext(), "Please enter your full name", Toast.LENGTH_SHORT).show();
        return false;
    }
});

//this is the email address field
semailTxt = (EditText) findViewById(R.id.txtSEmailAddress);
semailTxt.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) {

    }
});

semailTxt.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        Toast.makeText(getApplicationContext(), "Please enter the email address to be associated with your account", Toast.LENGTH_SHORT).show();
        return false;
    }
});

//this is the password field
spasswordTxt = (EditText) findViewById(R.id.txtSPassword);
    spasswordTxt.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) {
        //the code to enable the button. This needs the most optimization
        if (String.valueOf(spasswordTxt.getText()).trim().length() > 3 && 
            String.valueOf(snameTxt.getText()).trim().length() > 0 && 
            String.valueOf(semailTxt.getText()).trim().length() > 0) {
            signupBtn.setEnabled(true);
        } else {
            signupBtn.setEnabled(false);
        }
        if (String.valueOf(spasswordTxt.getText()).trim().length() < 3) {
            signupBtn.setEnabled(false);
        }
        if (String.valueOf(snameTxt.getText()).trim().length() < 1) {
            signupBtn.setEnabled(false);
        }
        if (String.valueOf(semailTxt.getText()).trim().length() < 1) {
            signupBtn.setEnabled(false);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

spasswordTxt.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        Toast.makeText(getApplicationContext(), 
            "Please enter a password with a minimum of 4 characters", Toast.LENGTH_SHORT).show();
        return false;
    }
});

spasswordconfirmTxt = (EditText) findViewById(R.id.txtSPasswordConfirm);
spasswordconfirmTxt.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) {
        //if the password and the password confirm fields do not match 
        //then the password confirm field goes red. This also needs optimization
        if (spasswordconfirmTxt.getText().toString().equals(spasswordTxt.getText().toString())) {
            spasswordconfirmTxt.getBackground().clearColorFilter();
        }
        else {
            spasswordconfirmTxt.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
            signupBtn.setEnabled(false);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

 //this allows the password field characters to be shown
final CheckBox showPasswordCheckBox = (CheckBox) findViewById(R.id.showPasswordCheckBox);
showPasswordCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(!isChecked){
            spasswordTxt.setTransformationMethod(PasswordTransformationMethod.getInstance());
            spasswordconfirmTxt.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        else {
            spasswordTxt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            spasswordconfirmTxt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }
});

最佳答案

我建议你使用一些方便的方法,这将帮助你写出更清晰的代码。例如,使用此方法检查字段是否包含有效的 strihg:

/ Checks if a String actually carries information
public static boolean isValidString(String string) {
    if (string == null) {
        return false;
    } else {
        String s = string.trim(); // Remove blank spaces at start and at end
        if ((s.length() == 0) || (s.equalsIgnoreCase(""))) return false;
        else return true;
    }
}

具体针对您的问题,我看到字段的实际验证是在密码字段的 onTextChanged(...) 方法中完成的,因此很明显填写密码字段last 将执行验证,否则如果您首先填写密码然后转到其他字段,它们的 onTextChanged(...) 根本不执行任何操作,因此不会执行验证。您应该添加一个在每个 onTextChanged(...) 回调中调用的方法!

对于红色区域,我不确定这是否是正确的做法,只是尝试使用一些不同的方式来提供视觉线索,See my blog post例如。 但是,我看到您没有进行检查,因此当 onTextChanged(...) 在确认字段中被触发时,您没有正确启用提交按钮,我想您忘记了什么:

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    //if the password and the password confirm fields do not match 
    //then the password confirm field goes red. This also needs optimization
    if (spasswordconfirmTxt.getText().toString().equals(spasswordTxt.getText().toString())) {
        spasswordconfirmTxt.getBackground().clearColorFilter();
        signupBtn.setEnabled(true); // Enable the button if everything is ok
    }
    else {
        spasswordconfirmTxt.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
        signupBtn.setEnabled(false);
    }
}

所以最重要的是,创建另一个方法,您可以从中一起检查所有字段,如果所有检查都正常,该方法也将启用该按钮,并在 onTextChanged(... ) 回调被触发!

干杯!

关于android - EditText 的验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33064373/

相关文章:

android - 自定义键盘不应用颜色更改的问题

android - Android Studio如何在 Activity 开始时自动播放背景音乐

android - 我的图像缓存进程会泄漏内存吗?

android - 多次启动IntentService android

android - 如何在 CardView 的自定义形状中隐藏阴影?

java - 如何直接从复制或分享 URL 获取 Instagram 图片或视频 URL?

android - 以编程方式将 layout_bellow 属性字段更改为另一个字段

java - Android 如何将.raw数据转换为.mp3文件?

php - 将android连接到免费虚拟主机中的服务器

android - 使用 PlaybackOverlayFragment 隐藏当前时间