2017-09-16 3 views
0

아니요 EditText을 실행 중일 때 onCreate()에서 실행 중지합니다. 그러나 CheckBox을 클릭하면 메서드가 한 번 실행 된 다음 다시 실행을 중지합니다.CheckBOx가 작동하지 않는 상태에서 EditTExt를 사용 또는 사용하지 않음

사람은 대답

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

당신은 checkBoxsetOnCheckedChangeListener 내부에 EditText을 초기화하는을 찾는 데 도움이. 그것이 작동하지 않는 이유입니다.

EditText (extraSpace)을 oncreate()으로 초기화하거나 EXtraWorkCalc() 메서드를 호출하기 전에 초기화하십시오.

이동 onCreate()이 라인 :.

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

YOR 오른쪽 경하지만 내가 좋아하는 유효성 검사 다음에서 onCreate에서 초기화() 및 AM 경우 경우 (TextUtils.isEmpty (extraSpace.getText의 toString()())) { extraSpace.setError ("추가 작업을 입력하십시오"); } CheckBox Listner의 If() 문이 작동하지 않고 Number Format Exception 오류를 표시합니다. –

관련 문제