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

但是,它不能按预期方式工作。为了启用注册按钮,必须按以下顺序填写字段:从上到下(这不是故意的)。

此外,除非显示字符,否则确认密码字段上的红色不会消失,并且注册按钮也不会启用。检查是否在所有字段中都有字符似乎无法正常工作,我不确定为什么。该检查有时有效,而其他时间则无效。

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

这是布局:

<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(...)方法中完成的,因此很明显,最后填写密码字段将执行验证,否则,如果您填写密码首先,然后转到其他字段,它们的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(...)回调时调用该方法!

干杯!

07-28 06:09