2014-11-18 5 views
0

버튼을 누를 때마다 텍스트가 변경되고 불리언 값이 변경되지만 작동하지 않습니다. 어떤 도움이 필요합니까? Thnks는버튼을 눌렀을 때 상태가 변하지 않습니다.

에서 onCreate 전에

:

public boolean status = false; 

버튼 클릭

case R.id.saveButton: 

if (status != true) { 
    currentStatus.setText("Current status: In need of help!"); 
    status = true; 
} 
if (status != false) { 
    currentStatus.setText("Current status: Fine."); 
    status = false; 
} 
break; 
+0

에서 왜! = 거짓 View.OnClickListener 구현? 'if'에 상태를 직접 넣으십시오 – Darpan

+0

더 많은 코드를 게시하십시오 ... –

+0

우리는 더 많은 정보가 필요합니다. 이 코드는 어떻게 호출됩니까? 귀하의 텍스트가 바뀌 었습니까? 디버거를 사용해 보셨습니까? – jhamon

답변

3
if(status){ 
     currentStatus.setText("Current status: Fine."); 
     status = false; 
}else {        
     currentStatus.setText("Current status: In need of help!"); 
     status = true 
} 

또는 삼항 연산자를 사용 :

currentStatus.setText(status ? "Current status: Fine." : "Current status: In need of help!"); 
status = !status;//toggle boolean value 
+0

고맙습니다. 우리는 지체 있습니다. – Paramone

+1

일어납니다, 우리는 당신이 보는 필사자입니다. – Darpan

+0

Sergey 코드를 추가해 주셔서 감사합니다. 삼자 연산자를 피해야한다고 들었지만. 글쎄, 그것은 또 다른 질문을 구성 할 것이다. – Darpan

0

이 시도 : 당신은 실제로 두 번 상태를 변경 나는 귀하의 코드.

case R.id.saveButton: 

      if(status == false){ 
       currentStatus.setText("Current status: In need of help!"); 

       status = true; 

      } 
      else if(status == true){ 
       currentStatus.setText("Current status: Fine."); 
       status = false; 

      } 
     break; 
0

수정 된 코드 : 당신은 당신의 코드에서 첫 if 문 뒤에 else을 사용해야합니다.

 boolean status = false; 

     System.out.println("status : "+status); // False 

     if(status != true){ 
      status = true; 
     }else if(status != false){ // add else here to resolve issue 
      status = false; 
     } 

     System.out.println("status : "+status); // True 
+0

코드 조각에 설명을 추가하십시오. – SilentKiller

+0

@SilentKiller 의견을 보내 주시면 감사하겠습니다! 코드에 설명을 추가했습니다. –

1

경우 조건 일 부울 상태 그렇게 할 때 체크하면 조건 부울 변수를 == 또는 = 연산자 간단한 넣어 부울 변수는 경우 작은 브라켓에 사용할 필요 사용하지!

if(status){ 
// this code executed when status is value is - true 
}else{ 
// this code executed when status is value is - false 
} 
0

난 당신이 didn를 생각을 버튼의 setOnClickListener를 호출하지 마십시오.

인터페이스

Button saveButton = (Button)findViewById(R.id.saveButton); 
saveButton.setOnClickListener(this); 

그리고

@Override 
public void onClick(View v) { 

    if(status){ 
    currentStatus.setText("Current status: Fine."); 
    status = false; 
    }else {        
    currentStatus.setText("Current status: In need of help!"); 
    status = true 
    } 
} 
관련 문제