android - Button 一直按不到
ringa_lee
ringa_lee 2017-04-17 18:00:57
[Android讨论组]

我的 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的,那肯定按不到了。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号