我的布局文件中有如下按钮和文本字段
<EditText
android:id="@+id/et_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView4"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/btn_app_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_password"
android:layout_centerHorizontal="true"
android:layout_marginTop="63dp"
android:background="@drawable/button"
android:text="SIGN UP"
android:textColor="@color/white" />
如何将此背景更改为 @drawable/button_success
如果所有编辑文本字段都填充了我的 Activity 类,如下所示
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
btn_app_signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = et_name.getText().toString();
String email = et_email.getText().toString();
String password = et_password.getText().toString();
registerAccount(name, email, password);
}
});
}
private void registerAccount(String name, String email, String password) {
if(TextUtils.isEmpty(name) || TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {
Toast.makeText(SignupActivity.this, "All fields required", Toast.LENGTH_LONG).show();
}
else {
//save data in datbase
}
}
如果我填写所有编辑文本字段填充更改按钮按钮背景,我该怎么做
最佳答案
试试这个
private void registerAccount(String name, String email, String password) {
if(!TextUtils.isEmpty(name)&& !TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)){
btn_app_signup.setBackground(ContextCompat.getDrawable(this,R.drawable.ic_android_black_24dp));// set here your backgournd to button
// save data in database
}else {
Toast.makeText(this, "All fields required", Toast.LENGTH_LONG).show();
}
}
编辑
et_password.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) {
}
@Override
public void afterTextChanged(Editable editable) {
if (!TextUtils.isEmpty(et_name.getText().toString()) && !TextUtils.isEmpty(et_email.getText().toString()) && !TextUtils.isEmpty(et_password.getText().toString())) {
btn_app_signup.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_android_black_24dp));// set here your backgournd to button
}else {
btn_app_signup.setBackgroundResource(android.R.drawable.btn_default);
}
}
});
et_email.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) {
}
@Override
public void afterTextChanged(Editable editable) {
if (!TextUtils.isEmpty(et_name.getText().toString()) && !TextUtils.isEmpty(et_email.getText().toString()) && !TextUtils.isEmpty(et_password.getText().toString())) {
btn_app_signup.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_android_black_24dp));// set here your backgournd to button
}else {
btn_app_signup.setBackgroundResource(android.R.drawable.btn_default);
}
}
});
et_name.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) {
}
@Override
public void afterTextChanged(Editable editable) {
if (!TextUtils.isEmpty(et_name.getText().toString()) && !TextUtils.isEmpty(et_email.getText().toString()) && !TextUtils.isEmpty(et_password.getText().toString())) {
btn_app_signup.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_android_black_24dp));// set here your backgournd to button
}else {
btn_app_signup.setBackgroundResource(android.R.drawable.btn_default);
}
}
});
关于android - 更改填充所有编辑文本字段的按钮颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47281135/