1

I have 3 CheckBoxes and next to each of them is an EditText for data-entry. How do I activate and deactivate the EditText next to each CheckBox depending on whether the CheckBox is marked or not? Sorry for my English.

enter image description here

public class MainActivity extends AppCompatActivity {

    DecimalFormat df = new DecimalFormat("##,##");

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

    public void calcularMedia(View v) {
        try {
            EditText notaP1 = (EditText) findViewById(R.id.etP1);
            EditText notaP2 = (EditText) findViewById(R.id.etP2);
            EditText notaP3 = (EditText) findViewById(R.id.etP3);

            TextView tvNotaFinal = (TextView) findViewById(R.id.tvNotaMedia);
            tvNotaFinal.setText("");

            if (notaValida(notaP1.getText().toString(), notaP2.getText().toString(), notaP3.getText().toString())) {
                String cad = "PUNTUAIÓN DEL ALUMNO: " + df.format(calculoNota(notaP1.getText().toString(), notaP2.getText().toString(),
                        notaP3.getText().toString()));
                TextView texto = (TextView) findViewById(R.id.tvNotaMedia);
                texto.setText(cad);
            } else {
                Toast tError = Toast.makeText(getApplicationContext(), "Las notas no pueden ser superiores a 10", Toast.LENGTH_SHORT);
                tError.setGravity(Gravity.CENTER_HORIZONTAL, 0, 600);
                tError.show();
            }
        } catch (Exception ex) {
            Toast tError = Toast.makeText(getApplicationContext(), "Faltan campos por rellenar.", Toast.LENGTH_SHORT);
            tError.setGravity(Gravity.CENTER_HORIZONTAL, 0, 600);
            tError.show();
        }
    }

    public int calculoNota(String notaP1, String notaP2, String notaP3) {
        int notaFinal = (Integer.parseInt(notaP1) + Integer.parseInt(notaP2) + Integer.parseInt(notaP3)) / 3;
        return notaFinal;
    }

    public boolean notaValida(String examen, String practicas, String actitud) {
        if (Double.parseDouble(examen) <= 10 && Double.parseDouble(practicas) <= 10 && Double.parseDouble(actitud) <= 10) {
            return true;
        }
        return false;
    }

}
BOWIEKNIFE
  • 11
  • 2
  • You should look at the "How to ask a question" link https://stackoverflow.com/help/how-to-ask . Code that you have tried and a sample product would help https://stackoverflow.com/help/minimal-reproducible-example – lorem ipsum Oct 16 '20 at 14:56

3 Answers3

0

Are you looking for how to disable an EditText or how to tell whether a CheckBox is marked or not?

For the CheckBox marked or not check: https://developer.android.com/guide/topics/ui/controls/checkbox

For the EditText disabling: Disabling of EditText in Android

0

use this code to achieve

  if(checkBox1.isChecked()){
        editText1.setEnabled(true);

// do the stuff you want to do when your checkbox is checked

    }else{
    editText1.setEnabled(false);

// do the stuff you want to do when your checkbox is not checked

}

Repeat the code for the other checkBoxes and other editTexts and that's it

Harsh Kothari
  • 439
  • 4
  • 8
0

I'd propose you set an onCheckChangedListener for each of the Checkboxes and then activate/deactivate the EditTexts based on this. This seems like the most straightforward approach. You can do something like this:

KOTLIN:

checkBox1.setOnCheckedChangeListener { _, isChecked -> edittext1.isEnabled = isChecked }
checkBox2.setOnCheckedChangeListener { _, isChecked -> edittext2.isEnabled = isChecked }
checkBox3.setOnCheckedChangeListener { _, isChecked -> edittext3.isEnabled = isChecked }

JAVA:

checkBox1.setOnCheckedChangeListener((buttonView, isChecked) -> {
            editText1.setEnabled(isChecked);
        });
checkBox2.setOnCheckedChangeListener((buttonView, isChecked) -> {
            editText2.setEnabled(isChecked);
        });
checkBox3.setOnCheckedChangeListener((buttonView, isChecked) -> {
            editText3.setEnabled(isChecked);
        });

This way, each of your EditTexts would always be enabled when their corresponding checkbox is checked.

Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
  • I don't understand the syntax of your solution; I have added the main code. Could you implement your solution regarding my code? – BOWIEKNIFE Oct 16 '20 at 15:40