2017-09-16 100 views
0

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

回答

1

您初始化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()語句不工作並給我數字格式異常的錯誤 –