lundi 2 février 2015

Change activity's behavior with checkbox

So, I'm attempting to change the equations with a checkbox, I am attaching a sample of code to see if anyone can tell me what I am doing wrong. With this code, I am simply trying to not make any calculations when the checkbox is checked, and to automatically calculate and populate the other fields when the checkbox is not checked. It works perfectly until I check, uncheck, and recheck the checkbox. I can verify it's status with my textview, but It continues to focus on the textviews when checked or unchecked... Thank you for any help!


public class MainActivity extends 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();
}
}
}
});
}
}
});
}


}





Aucun commentaire:

Enregistrer un commentaire