android - Button 一直按不到

查看:123
本文介绍了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. 求大侠帮帮忙!

解决方案

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

这篇关于android - Button 一直按不到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆