启用或禁用EditTExt与CheckBOx不工作

问题描述:

禁用EditText在执行onCreate()。但是,当点击CheckBox时,该方法执行一次,然后再次停止执行。启用或禁用EditTExt与CheckBOx不工作

任何人的帮助,以找到答案

public void EXtraWorkCalc(){ 
    extraworkCheck = (CheckBox)findViewById(R.id.workCheck); 
    extraworkCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton compoundButton, boolean b) { 
      if (!compoundButton.isChecked()) { 
       extraSpace = (EditText) findViewById(R.id.extraWidthEtxt); 
       extraSpace.setEnabled(false); 
       extraSpace.setBackgroundColor(Color.LTGRAY); 
      } else 
       extraSpace.setEnabled(true); 
       extraSpace.setBackgroundColor(Color.WHITE); 
     } 
    }); 
} 

您初始化EditText在里面checkBoxsetOnCheckedChangeListener。这就是为什么它不起作用。

oncreate()或调用EXtraWorkCalc()方法之前初始化您的EditText(extraSpace)。

移动此行onCreate()

extraSpace = (EditText) findViewById(R.id.extraWidthEtxt); 
+0

尤尔是正确的,但爵士当我初始化它的onCreate(),然后验证一样,如果(TextUtils.isEmpty(extraSpace.getText()的toString()) ){ extraSpace.setError(“请输入额外的工作”); }然后在CheckBox Listner中的If()语句不工作并给我数字格式异常的错误 –