启用或禁用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
在里面checkBox
setOnCheckedChangeListener
。这就是为什么它不起作用。
在oncreate()
或调用EXtraWorkCalc()
方法之前初始化您的EditText
(extraSpace)。
移动此行onCreate()
:
extraSpace = (EditText) findViewById(R.id.extraWidthEtxt);
尤尔是正确的,但爵士当我初始化它的onCreate(),然后验证一样,如果(TextUtils.isEmpty(extraSpace.getText()的toString()) ){ extraSpace.setError(“请输入额外的工作”); }然后在CheckBox Listner中的If()语句不工作并给我数字格式异常的错误 –