更改按钮颜色填充所有edittext字段?

时间:2017-11-14 08:40:42

标签: android button

我的布局文件中有按钮和文本字段,如下所示

<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" />

如果所有编辑文本字段都填充了我的Activity类,如何将此背景更改为@drawable/button_success

@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
        }
    }

如果我填写所有编辑文本字段填写更改按钮按钮背景

,我该怎么办?

3 个答案:

答案 0 :(得分:1)

试试这个

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);
            }
        }
    });

答案 1 :(得分:1)

et_name.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){
    String name=et_name.getText().toString();
    String email=et_email.getText().toString();
    String password=et_password.getText().toString();

    valiadte(name,email,password);
    }

    @Override
    public void afterTextChanged(Editable s){

    }
    });

    et_email.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){
    String name=et_name.getText().toString();
    String email=et_email.getText().toString();
    String password=et_password.getText().toString();

    valiadte(name,email,password);
    }

    @Override
    public void afterTextChanged(Editable s){

    }
    });
    et_password.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){
    String name=et_name.getText().toString();
    String email=et_email.getText().toString();
    String password=et_password.getText().toString();

    valiadte(name,email,password);
    }

    @Override
    public void afterTextChanged(Editable s){

    }
    });

并使函数无效

 private void valiadte(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     
  } 

 }

答案 2 :(得分:0)

您只需使用drawable更改背景:

btn_app_signup.setBackground(getResources().getDrawable(R.dr‌​awable.button_succes‌​s));

检查文本时:

if(et_name.getText().toString().equals("") || et_email.getText().toString().equals("") || et_password.getText().toString().equals("")) {
    Toast.makeText(SignupActivity.this, "All fields required", Toast.LENGTH_LONG).show();
}

希望它有所帮助。