使用复选框更改活动的行为

时间:2015-02-03 02:47:44

标签: android checkbox

所以,我试图用复选框更改方程式,我附上一个代码示例,看看是否有人可以告诉我我做错了什么。使用此代码,我只是在选中复选框时尝试不进行任何计算,并在未选中复选框时自动计算和填充其他字段。它完美运行,直到我检查,取消选中并重新检查复选框。我可以使用我的textview验证它的状态,但在选中或取消选中时,它会继续关注文本视图...感谢您的帮助!

公共类MainActivity扩展了ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText editIn = (EditText) findViewById(R.id.editIn);
    final EditText editFt = (EditText) findViewById(R.id.editFt);
    final EditText editYd = (EditText) findViewById(R.id.editYd);
    final TextView checkBoxChecked = (TextView) findViewById(R.id.checkBoxChecked);
    final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);


    checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (checkBox.isChecked()) {
                checkBoxChecked.setText("Check Box Checked");

            } else {
                checkBoxChecked.setText("Check Box NOT Checked!");
                editIn.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int i, int i2, int i3) {
                    }

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

                    @Override
                    public void afterTextChanged(Editable s) {
                        if (editIn.hasFocus()) {

                            try {
                                double in = Double.valueOf(editIn.getText().toString());
                                double ft = in / 12;
                                double yd = in / 36;
                                editFt.setText(String.format("%.2f", (ft)));
                                editYd.setText(String.format("%.2f", (yd)));
                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
                editFt.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int i, int i2, int i3) {
                    }

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

                    @Override
                    public void afterTextChanged(Editable s) {
                        if (editFt.hasFocus()) {
                            try {
                                double ft = Double.valueOf(editFt.getText().toString());
                                double in = ft * 12;
                                double yd = ft / 3;
                                editIn.setText(String.format("%.2f", (in)));
                                editYd.setText(String.format("%.2f", (yd)));
                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
                editYd.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int i, int i2, int i3) {
                    }

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

                    @Override
                    public void afterTextChanged(Editable s) {
                        if (editYd.hasFocus()) {
                            try {
                                double yd = Double.valueOf(editYd.getText().toString());
                                double in = yd * 36;
                                double ft = yd * 3;
                                editIn.setText(String.format("%.2f", (in)));
                                editFt.setText(String.format("%.2f", (ft)));
                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
            }
        }
    });
}

}

1 个答案:

答案 0 :(得分:0)

您需要添加setOnCheckedChangeListener,而不是onCLickListener。您还需要在OnTextChanged

中移动方程式
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final EditText editIn = (EditText) findViewById(R.id.editIn);
    final EditText editFt = (EditText) findViewById(R.id.editFt);
    final EditText editYd = (EditText) findViewById(R.id.editYd);
    final TextView checkBoxChecked = (TextView) findViewById(R.id.checkBoxChecked);
    final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
     checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    checkBoxChecked.setText("Check Box Checked");
                  editIn.setText("");
                  editIn.setEnabled(false);
                  editIn.setFocusable(false);
                  editFt.setText("");
                  editFt.setEnabled(false);
                  editFt.setFocusable(false);
                  editYd.setText("");
                  editYd.setEnabled(false);
                  editYd.setFocusable(false);
                } else {
                 editIn.setEnabled(true);
                 editIn.setFocusable(true);
                 editFt.setEnabled(true);
                 editFt.setFocusable(true);
                 editYd.setEnabled(true);
                 editYd.setFocusable(true);
                 checkBoxChecked.setText("Check Box NOT Checked!");

                  editIn.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int i, int i2, int i3) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int i, int i2, int i3) {
                       try {
                            double in = Double.valueOf(editIn.getText().toString());
                            double ft = in / 12;
                            double yd = in / 36;
                            editFt.setText(String.format("%.2f", (ft)));
                            editYd.setText(String.format("%.2f", (yd)));
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });
                editFt.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int i, int i2, int i3) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int i, int i2, int i3) {
                     try {
                            double ft = Double.valueOf(editFt.getText().toString());
                            double in = ft * 12;
                            double yd = ft / 3;
                            editIn.setText(String.format("%.2f", (in)));
                            editYd.setText(String.format("%.2f", (yd)));
                        } catch (NumberFormatException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });
                editYd.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int i, int i2, int i3) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int i, int i2, int i3) {
                    try {
                                double yd = Double.valueOf(editYd.getText().toString());
                                double in = yd * 36;
                                double ft = yd * 3;
                                editIn.setText(String.format("%.2f", (in)));
                                editFt.setText(String.format("%.2f", (ft)));
                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                            }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });
                }
            }
        });


}

更新尝试此操作,让我知道每次检查都会显示吐司并取消选中

    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                Toast.makeText(MainActivity.this, "Checked", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "Not-Checked", Toast.LENGTH_SHORT).show();
            }
        }
    });