2014-03-05 2 views
0

이것은 매우 간단한 프로그램입니다. 지금은 간단하게 유지하십시오. 개념은 간단합니다. 모델 건물에서 널리 사용되는 다양한 비늘에 대한 라디오 버튼이 있습니다. 해당 라디오 버튼을 클릭하고 실제 크기를 인치로 입력 한 다음 계산을 누르면 인치로 표시됩니다. 단순한 부서지만, 일할 수만 있다면 필요한 정보를 얻을 수 있습니다. 지금까지는 깔끔하게 컴파일되고 라디오 버튼을 확인하고 숫자를 입력 한 다음 계산 버튼을 누르면 아무 것도하지 않습니다. 다음은 내가 추가 한 코드입니다 (물론 수입은 포함되지 않습니다).오류가없고 프로그램이 실행되지만 절대적으로 않습니다. JACK

public void onButtonClick(View v) 
{ 
    int n1, result = 0; 
    EditText e1 = (EditText)findViewById(R.id.editTextSize); 
    TextView t1 = (TextView)findViewById(R.id.textView3); 
    RadioButton rb144 = (RadioButton)findViewById(R.id.radioButton144); 
    RadioButton rb72 = (RadioButton)findViewById(R.id.radioButton72); 
    RadioButton rb48 = (RadioButton)findViewById(R.id.radioButton48); 
    RadioButton rb35 = (RadioButton)findViewById(R.id.radioButton35); 
    RadioButton rb32 = (RadioButton)findViewById(R.id.radioButton32); 
    RadioButton rb25 = (RadioButton)findViewById(R.id.radioButton25); 
    RadioButton rb24 = (RadioButton)findViewById(R.id.radioButton24); 
    n1 = Integer.parseInt(e1.getText().toString()); 
    if(rb144.isChecked()){ 
     result = n1/144; 
    }else if(rb72.isChecked()){ 
     result = n1/72; 
    }else if(rb48.isChecked()){ 
     result = n1/48; 
    }else if(rb35.isChecked()){ 
     result = n1/35; 
    }else if(rb32.isChecked()){ 
     result = n1/32; 
    }else if(rb25.isChecked()){ 
     result = n1/25; 
    }else if(rb24.isChecked()){ 
     result = n1/24; 
    } 
    t1.setText(Integer.toString(result)); 
+3

쪽지 : 정수 나누기. –

+0

들어가는 진정한 가치는 무엇입니까? 어떤 라디오 버튼이 설정되어 있습니까? – paxdiablo

+0

onButtonClick() 메서드를 호출하는 코드 게시 –

답변

0

OnClickListener()의 메서드 이름이 잘못되었습니다. onButtonClick (...)이 아니라 onClick (...)이어야합니다. 예를 참조하십시오 : 앞으로

Button button = (Button) findViewById(R.id.button1); 

    button.setOnClickListener(new OnClickListener() { 

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

     TextView tv = (TextView)findViewById(R.id.textView3); 
     tv.setText("hello button!"); 

    } 
    }); 

하면 이러한 방법을 정의 할 때 컴파일러는 당신이 실제로 뭔가를 오버라이드 (override)되지 않는 한 것을 감지 뭔가를 잘못 입력하는 경우, 그 방법을 @Override 주석을 추가해야합니다.

0

다음 코드를 사용하십시오.

코드에 몇 가지 변경 사항이 있습니다.

public void onButtonClick(View v) 
{ 
     float n1,result = 0; 
     EditText e1 = (EditText)findViewById(R.id.editTextSize); 
     TextView t1 = (TextView)findViewById(R.id.textView3); 

     RadioButton rb144 = (RadioButton)findViewById(R.id.radioButton144); 
     RadioButton rb72 = (RadioButton)findViewById(R.id.radioButton72); 
     RadioButton rb48 = (RadioButton)findViewById(R.id.radioButton48); 
     RadioButton rb35 = (RadioButton)findViewById(R.id.radioButton35); 
     RadioButton rb32 = (RadioButton)findViewById(R.id.radioButton32); 
     RadioButton rb25 = (RadioButton)findViewById(R.id.radioButton25); 
     RadioButton rb24 = (RadioButton)findViewById(R.id.radioButton24); 

     n1 =Float.parseFloat(e1.getText().toString()); 

     if(rb144.isChecked()){ 
      result = n1/144; 
     }else if(rb72.isChecked()){ 
      result = n1/72; 
     }else if(rb48.isChecked()){ 
      result = n1/48; 
     }else if(rb35.isChecked()){ 
      result = n1/35; 
     }else if(rb32.isChecked()){ 
      result = n1/32; 
     }else if(rb25.isChecked()){ 
      result = n1/25; 
     }else if(rb24.isChecked()){ 
      result = n1/24; 
     } 
     t1.setText(String.valueOf(result)); 
} 

참고 : 더 나은 프로그래밍 방법 당신은 글고 치기 등에서 onCreate에서 라디오 버튼처럼 뷰의 ID를 찾는 물건을 정의 할 태어나 셨() 대신에 버튼 클릭에 대한의.

관련 문제