我有一个注册屏幕。它包含用户的全名,电子邮件地址和密码的位置(用户必须确认其密码)。
但是,它没有按预期工作。要启用注册按钮,必须按顺序填写字段:从上到下(这不是预期的)。
此外,确认密码字段上的红色不会消失,除非显示字符,否则注册按钮不会启用。检查所有字段中是否有字符似乎都出现故障,我不知道为什么。检查有时会起作用,但在其他时候不起作用。
请有人帮我优化一下。代码中有注释以显示每种方法的作用。
这是布局:
<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());
}
}
});
答案 0 :(得分:1)
我建议你使用一些方便的方法,这可以帮助你编写更清晰的代码。例如,使用此方法检查字段是否包含有效的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(...)
个回调被触发时调用该方法!
干杯!