Я просмотрел рекомендованные похожие вопросы и перепробовал много разных подходов, но пока они не сработали, и я не уверен, почему. Может быть, я реализовал их неправильно, но не уверен.
То, что я пытаюсь сделать, - это изменить рисунок кнопки на более яркий цвет, если два поля Edittext оба go с пустого на текст в них.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".RegistrationActivity"
android:orientation="vertical">
<TextView
android:layout_width="204dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:layout_marginTop="33dp"
android:text="My email and password are"
android:textSize="32dp"
android:fontFamily="@font/roboto_black"
android:textColor="@android:color/holo_green_light" />
<EditText
android:id="@+id/emailReg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:fontFamily="@font/roboto_lightitalic"
android:hint="email"
android:inputType="textEmailAddress"
android:layout_marginLeft="33dp"
android:layout_marginTop="27dp"/>
<EditText
android:id="@+id/passwordReg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="password"
android:ems="10"
android:fontFamily="@font/roboto_lightitalic"
android:inputType="textWebPassword"
android:layout_marginLeft="33dp"
android:layout_marginTop="17dp"/>
<TextView
android:id="@+id/textView"
android:layout_width="323dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:layout_marginTop="23dp"
android:ems="10"
android:justificationMode="inter_word"
android:text="Clicking 'Continue' will send a verification email to the address above. This will be used to log you in at future dates." />
<ImageButton
android:id="@+id/registerReg"
android:layout_width="wrap_content"
android:layout_height="52dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="17dp"
android:layout_marginRight="32dp"
android:background="@null"
android:scaleType="fitCenter"
/>
</LinearLayout>
и операция:
public class RegistrationActivity extends AppCompatActivity {
//VARIABLES
private ImageButton mRegister;
private EditText mEmail;
private EditText mPassword;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener fireBaseAuthStateListener;
private DatabaseReference userRef;
String emailStr;
String passwordStr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
//VARIABLE IDS
mEmail = (EditText) findViewById(R.id.emailReg);
mPassword = (EditText) findViewById(R.id.passwordReg);
mRegister = (ImageButton) findViewById(R.id.registerReg);
mAuth = FirebaseAuth.getInstance();
mEmail.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
emailStr = mEmail.getText().toString().trim();
}
}
});
mPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
passwordStr = mPassword.getText().toString().trim();
}
}
});
changeButtonDrawable(emailStr, passwordStr);
//REGISTER BUTTON
mRegister.setOnClickListener(new View.OnClickListener() {
final String email = mEmail.getText().toString().trim();
final String password = mPassword.getText().toString().trim();
@Override
public void onClick(View v) {
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(RegistrationActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
firebaseUtility fU = new firebaseUtility();
String currentUserId = fU.getUserIdString();
DatabaseReference userRef = fU.getUserDatabaseRef(currentUserId);
Map userInfo = new HashMap();
userInfo.put("email", email);
userInfo.put("profileImageUrl", "default");
userRef.updateChildren(userInfo);
if (task.isSuccessful()) {
Intent intent = new Intent(RegistrationActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
});
}
});
}
@Override
public void onStart() {
super.onStart();
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if(user != null) {
Intent intent = new Intent(RegistrationActivity.this, NameAgeZipActivity.class);
startActivity(intent);
finish();
}
}
private void changeButtonDrawable (String email, String password){
if (TextUtils.isEmpty(email) && TextUtils.isEmpty(password)){
mRegister.setImageResource(R.drawable.continue_btn_grey_on_grey);
} else if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)) {
mRegister.setImageResource(R.drawable.continue_btn_white_on_purple);
}
}
}
По сути, ImageButton должен измениться на drawable continue_btn_white_on_purple, если в обоих полях есть текст, но сейчас он просто остается серым. Я пытался понять это некоторое время. Любая помощь высоко ценится.