2012-05-21 2 views
-3

그래서 내가 달성하기 위해 노력하고있는 것은 바로 다음과 같이해야한다 :도 가능 그게 전부 내가 도움이나 아이디어의 모든 종류의 감사하겠습니다 경우 http://www.javascriptbank.com/simple-javascript-auto-sum-with-checkboxes.html/en/자동 합계 확인란

불행하게도 메신저 슈어 없습니다.

CheckBox chkone; 
CheckBox chktwo; 
CheckBox chkthree; 
TextView tv; 
OnClickListener checkBoxListener; 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    chkone = (CheckBox)findViewById(R.id.chk1); 
    chktwo = (CheckBox)findViewById(R.id.chk2); 
    thkthree = (CheckBox)findViewById(R.id.chk3); 


checkBoxListener = new OnClickListener() { 

     //@Override 
     public void onClick(View v) { 

      tv = (TextView)findViewById(R.id.tv1); 

      if (chkone.isChecked()) 
      { 
       // something 

      } 

     } 
    }; 

    chkone.setOnClickListener(checkBoxListener); 
    chktwo.setOnClickListener(checkBoxListener); 
    chkthree.setOnClickListener(checkBoxListener); 

답변

0

당신은 체크 박스의 상태 변경에 대한 알림을받을 대신 CompoundButton'sOnCheckedChangeListener 인터페이스를 구현해야합니다 : 내가 지금까지 가지고 advance.Some 것 감사드립니다.

숫자 값을 어딘가에 저장하여 작업을 쉽게하기 위해 CheckBox 클래스를 확장하지 않았다고 가정하면 체크 박스의 레이블 (텍스트)을 구문 분석하여 해당 값에 액세스하고 체크 된 상태를 기반으로해야합니다 증가/감소 합 : 당신이 그것을 예상대로

private TextView sumText; 
private float currentSum = 0; 
private final OnCheckedChangeListener checkBoxListener = 
    new OnCheckedChangeListener() 
{ 
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    { 
     if (isChecked) 
      currentSum += getNumber(buttonView); 
     else 
      currentSum -= getNumber(buttonView); 
     // TODO: you need to initialize this TextView in your 
     // onCreate method! 
     sumText.setText(Float.toString(currentSum)); 
    } 
}; 

private float getNumber(final CompoundButton chk) 
{ 
    try 
    { 
     return Float.parseFloat(chk.getText().toString()); 
    } 
    catch (NumberFormatException e) 
    { 
     return 0; 
    } 
} 

참고이에 대한 그 해당 CheckBox 예를 아래에 실제로 값을 검색 할 getNumber 방법을 수정해야 작동합니다!

+0

OOOOOO 완벽하게 작동 해 주셔서 감사합니다. – marko1234