suchen

Heim  >  Fragen und Antworten  >  Hauptteil

android - Button 一直按不到

我的 Activity 有三个 editText 和 一个 button. 如果三个editText都有字,button 就可以按,不然就按不到。问题是我的button一直按不到就算三个editText都有字。

Button buttonSave;

    public TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            // check Fields For Empty Values
            checkFieldsForEmptyValues();
        }
    };
    
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonSave = (Button) findViewById(R.id.buttonSave);
        buttonSave.setBackgroundColor(Color.parseColor("#D3D3D3"));
        buttonSave.setEnabled(false);
        editTextName = (EditText) findViewById(R.id.editTextName);
        editTextPassword = (EditText) findViewById(R.id.editTextPassword);
        editTextConfirm = (EditText) findViewById(R.id.editTextConfirmPassword);

        //Click Listener for button
        buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pass = editTextPassword.getText().toString();
                String confirm = editTextConfirm.getText().toString();
                String name = editTextName.getText().toString();
                if ((editTextName.getText().toString().trim().length() == 0) || (editTextPassword.getText().toString().trim().length() == 0) || (editTextConfirm.getText().toString().trim().length() == 0)) {
                    Toast.makeText(getApplicationContext(), "Field cannot be null", Toast.LENGTH_LONG).show();
                } 
            else if (!(pass.equals(confirm))) {
                    Toast.makeText(getApplicationContext(), "password and confirm password not same ", Toast.LENGTH_LONG).show();
                    Toast.makeText(getApplicationContext(), pass + confirm, Toast.LENGTH_SHORT).show();
                } 
                else
                 {
                        Intent intent = new Intent(MainActivity.this, AddMonthlyExpenses.class);
                        intent.putExtra("name", name);
                        startActivity(intent);
                  }
        });
    }

  public void checkFieldsForEmptyValues(){
        if((editTextName.getText().toString().trim().length() == 0) || (editTextPassword.getText().toString().trim().length() == 0) || (editTextConfirm.getText().toString().trim().length() == 0)){
            buttonSave.setEnabled(false);
            buttonSave.setBackgroundColor(Color.parseColor("#D3D3D3"));
        } else {
            buttonSave.setEnabled(true);
            buttonSave.setBackgroundColor(Color.parseColor("#FFAA66CC"));
        }
    }

还有 mTextWatcher never used. 求大侠帮帮忙!

ringa_leeringa_lee2772 Tage vor448

Antworte allen(1)Ich werde antworten

  • 高洛峰

    高洛峰2017-04-17 18:02:57

    buttonSave.setEnabled(false);,而你的mTextWatcher never used,buttonSave就永远是disable的,那肯定按不到了。

    Antwort
    0
  • StornierenAntwort