【问题标题】:Change activity's behavior with checkbox使用复选框更改活动的行为
【发布时间】:2015-02-03 02:47:43
【问题描述】:

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

公共类 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();
                            }
                        }
                    }
                });
            }
        }
    });
}

}

【问题讨论】:

    标签: android checkbox


    【解决方案1】:

    您需要添加一个 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();
                }
            }
        });
    

    【讨论】:

    • 是的,先生,我之前尝试过,但我复制并粘贴了您的代码,不幸的是,结果相同。如果您尝试它,它似乎可以工作,直到您循环复选框,然后它似乎将注意力集中在其他编辑文本上。非常感谢您的回复!我希望你能帮我解决这个问题...
    • 我会尽力的。当您选中和取消选中复选框时,准确告诉我您想要发生的事情
    • 我希望当我选中输入字段时没有任何反应的框时,稍后我可以添加一个方法。现在,它仍然在我循环复选框后计算
    • 因此,如果选中该复选框,您希望 editText 不执行任何操作。
    • 正确,我稍后再添加一个方法
    猜你喜欢
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    • 2010-10-04
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    相关资源
    最近更新 更多