【问题标题】:Activate EditText when checking a CheckBox on Android Studio在 Android Studio 上检查 CheckBox 时激活 EditText
【发布时间】:2020-10-16 23:39:05
【问题描述】:

我有 3 个复选框,每个复选框旁边是一个用于数据输入的 EditText。如何根据 CheckBox 是否被标记来激活和停用每个 CheckBox 旁边的 EditText?对不起我的英语。

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;
    }

}

【问题讨论】:

标签: android checkbox


【解决方案1】:

您是否正在寻找如何禁用 EditText 或如何判断 CheckBox 是否被标记?

对于CheckBox标记或不检查:https://developer.android.com/guide/topics/ui/controls/checkbox

对于 EditText 禁用:Disabling of EditText in Android

【讨论】:

    【解决方案2】:

    用这段代码来实现

      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
    

    }

    重复其他checkBoxes和其他editTexts的代码,就是这样

    【讨论】:

      【解决方案3】:

      我建议您为每个复选框设置一个onCheckChangedListener,然后根据此激活/停用 EditTexts。这似乎是最直接的方法。你可以这样做:

      科特林:

      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);
              });
      

      以这种方式,在检查相应的复选框时,每个递观都将始终启用。

      【讨论】:

      • 我不明白你的解决方案的语法;我已经添加了主要代码。您能否针对我的代码实施您的解决方案?
      猜你喜欢
      • 1970-01-01
      • 2016-04-20
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-06
      相关资源
      最近更新 更多