android - Button 一直按不到
ringa_lee
ringa_lee 2017-04-17 18:00:57
0
1
444

我的 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_lee
ringa_lee

ringa_lee

全部回覆(1)
小葫芦

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

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板