[英]How to change drawable of image button if two edit text fields are not empty
我查看了推荐的类似问题并尝试了许多不同的方法,但到目前为止它们还没有奏效,我不知道为什么。 也许我错误地实施了它们但不确定。
我想要做的是,如果两个 Edittext 字段都从空变为其中包含文本,则将按钮的 drawable 更改为更亮的颜色。
<?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 应该更改为可绘制的 continue_btn_white_on_purple,但现在它只是保持灰色。 一直试图弄清楚这一点。 非常感谢任何帮助。
为您的按钮制作一个可绘制的文件,如下所示
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape>
<!--if button is disabled it will show this color-->
<solid android:color="@color/disabledBtnBg" />
<corners android:radius="5dp" />
</shape>
</item>
<item>
<shape>
<solid android:color="#000000" />
<corners android:radius="5dp" />
</shape>
</item>
</selector>
现在将其设置为您的按钮
<Button
...
android:background="@drawable/primary_btn_bg"
...
/>
然后转到您的 Java 文件并添加以下内容:
if (TextUtils.isEmpty(email) && TextUtils.isEmpty(password)){
mRegister. mVerifyBtn.setEnabled(false);
else if (TextUtils.isEmpty(email) && TextUtils.isEmpty(password)){
mRegister. mVerifyBtn.setEnabled(true);
更实时的方法是使用 TextChangedListener
mEmail.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() != 0 && mPassword.getText().toString().length != 0) {
mRegister.setImageResource(R.drawable.continue_btn_grey_on_grey);
} else {
mRegister.setImageResource(R.drawable.continue_btn_white_on_purple);
}
}
public void afterTextChanged(Editable s) {
}
});
mPassword.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() != 0 && mEmail.getText().toString().length != 0) {
mRegister.setImageResource(R.drawable.continue_btn_grey_on_grey);
} else {
mRegister.setImageResource(R.drawable.continue_btn_white_on_purple);
}
}
public void afterTextChanged(Editable s) {
}
});
所以 imageButton 上的变化会立即反映出来
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.