【发布时间】: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();
}
}
}
});
}
}
});
}
}
【问题讨论】: