我正在尝试为我的应用创建“注册”页面,但无法在所有已正确填写的edittext字段中显示可绘制的“刻度线”。当前,可绘制对象仅显示在最后一个ConfirmPassword EditText中,而不显示在其他对象上,即使它们基本上具有相同的代码。这是我的XML文件-
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorPrimary"
android:theme="@style/LoginActivityStyle"
android:fitsSystemWindows="true"
tools:context=".RegisterActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="48dp"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:scaleType="fitCenter"
android:tint="@android:color/white"
android:scaleX="2.7"
android:scaleY="2.7"
android:src="@drawable/ic_interact_logo" />
<!-- Name Label -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<EditText android:id="@+id/register_first_name_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:hint="First Name" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<EditText android:id="@+id/register_last_name_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:hint="Last Name" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<!-- Email Label -->
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<EditText android:id="@+id/register_email_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Email" />
</android.support.design.widget.TextInputLayout>
<!-- Password Label -->
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<EditText android:id="@+id/register_password_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Password"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<EditText android:id="@+id/register_confirm_password_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="Confirm Password"/>
</android.support.design.widget.TextInputLayout>
<!-- Signup Button -->
<android.support.v7.widget.AppCompatButton
android:id="@+id/register_signup_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginBottom="24dp"
android:padding="12dp"
android:textSize="16sp"
android:background="@color/colorPrimaryDark"
android:text="Create Account"/>
</LinearLayout>
这是我的活动代码-
public class RegisterActivity extends AppCompatActivity {
private static final String TAG = "RegisterActivity";
private Button mSignUpButton;
private TextView mLoginLinkTextView;
private EditText mEmailEditText, mFirstNameEditText, mLastNameEditText,
mPasswordEditText, mConfirmPasswordEditText;
private Drawable mCorrectDrawable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mFirstNameEditText = findViewById(R.id.register_first_name_edit_text);
mLastNameEditText = findViewById(R.id.register_last_name_edit_text);
mEmailEditText = findViewById(R.id.register_email_edit_text);
mLoginLinkTextView = findViewById(R.id.register_login_text_view);
mCorrectDrawable = getResources().getDrawable(R.drawable.ic_correct, null);
mCorrectDrawable.setBounds(0,0, mCorrectDrawable.getIntrinsicWidth(), mCorrectDrawable.getIntrinsicHeight());
mCorrectDrawable.setTint(getResources().getColor(R.color.colorGreen, null));
mEmailEditText.setCompoundDrawablesWithIntrinsicBounds(0,0,0,0);
mFirstNameEditText.setCompoundDrawablesWithIntrinsicBounds(0,0,0,0);
mLastNameEditText.setCompoundDrawablesWithIntrinsicBounds(0,0,0,0);
mPasswordEditText.setCompoundDrawablesWithIntrinsicBounds(0,0,0,0);
mEmailEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(validateEmail()){
mEmailEditText.setError(null);
mEmailEditText.setCompoundDrawables(null, null, mCorrectDrawable, null);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
mFirstNameEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(validateFirstName()){
mFirstNameEditText.setError(null);
mFirstNameEditText.setCompoundDrawablesWithIntrinsicBounds(null, null, mCorrectDrawable2, null);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
mLastNameEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (validateLastName()){
mLastNameEditText.setError(null);
mLastNameEditText.setCompoundDrawablesWithIntrinsicBounds(null, null, mCorrectDrawable3, null);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
public boolean validateFirstName(){
boolean valid = true;
String first_name = mFirstNameEditText.getText().toString().trim();
if (first_name.isEmpty() || first_name.length() < 3) {
mFirstNameEditText.setCompoundDrawablesWithIntrinsicBounds(0,0,0,0);
mFirstNameEditText.setError("Enter at least 3 characters");
valid = false;
}
return valid;
}
public boolean validateLastName(){
boolean valid = true;
String last_name = mLastNameEditText.getText().toString().trim();
if (last_name.isEmpty() || last_name.length() < 3) {
mLastNameEditText.setCompoundDrawablesWithIntrinsicBounds(0,0,0,0);
mLastNameEditText.setError("Enter at least 3 characters");
valid = false;
}
return valid;
}
public boolean validateEmail(){
boolean valid = true;
String email = mEmailEditText.getText().toString();
if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
mEmailEditText.setCompoundDrawablesWithIntrinsicBounds(0,0,0,0);
mEmailEditText.setError("Enter a valid email address");
valid = false;
}
return valid;
}
}
请忽略登录,注册等。后端仍未设置。任何帮助,将不胜感激。谢谢!
编辑1 -首先,很抱歉不对代码进行注释。其次,我终于找到了导致此问题的原因。由于EditText.setError()语句,可绘制对象未显示。我停止使用这些,而是使用它来设置错误消息-
myEditText.setCompoundDrawablesWithIntrinsicBounds(0,0,0,0); //Remove all drawables from the edittext field
myInputLayout.setErrorEnabled(true);
myInputLayout.setError("Passwords do not match");
在这里,InputLayout是与该特定EditText字段相关联的输入。最后,显示可绘制对象-
myInputLayout.setErrorEnabled(false);
myEditText.setCompoundDrawablesWithIntrinsicBounds(null, null, mCorrectDrawable, null);
编辑2 -这是页面的样子-Registration Page 注意,我还编辑了代码,以便仅显示相关代码。很抱歉编辑不当。