2012-04-01 2 views
0

모든 것이 오류없이 작동하지만 xml에서는 tvfinalgrade가 0.0 ... 두 번 지느러미가 표시되지 않는 이유는 무엇입니까? 나는 tvfin 코드 라인이 어떤 순서로 작성되어야하는지 확신하지만, 나는 그것이 맞지 않다고 가정하고있다. 앱을 시작하면내 코드가 TextView를 올바르게 변경하지 못합니다.

public class GFActivity extends Activity { 
public double fin; 
@Override 
    public void onCreate(Bundle savedInstanceState) {   

     super.onCreate(savedInstanceState);  
     setContentView(R.layout.getfinal); 

     double q1, q2, ex; 
     EditText etq1, etq2, eteg; 
     etq1 = (EditText)findViewById(R.id.editText1); 
      try{ 
       q1 = Double.parseDouble(etq1.getText().toString()); 
      } catch (NumberFormatException e) { 
       q1=0; 
      } 
     etq2 = (EditText)findViewById(R.id.editText2); 
      try{ 
       q2 = Double.parseDouble(etq2.getText().toString()); 
      } catch (NumberFormatException e){ 
       q2 = 0; 
      } 
     eteg = (EditText)findViewById(R.id.editText3); 
     try{ 
      ex = Double.parseDouble(eteg.getText().toString()); 
     } catch (NumberFormatException e){ 
      ex = 0; 
     } 
     fin = 0.4*q1+0.4*q2+0.2*ex; 
      if(fin == (int)fin){ 
       System.out.println((int)fin); 
      } 
      else{ 
       fin = 0.01*((int)(fin*100)); 
       System.out.println(fin); 
      } 
      Button solve = (Button)findViewById(R.id.getfinbutton); 
      solve.setOnClickListener(new OnClickListener() { 

       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        TextView tvfin= TextView)findViewById(R.id.tvfinalgrade); 
         tvfin.setText(fin+""); 
       } 
      }); 


    } 
} 

답변

1

onCreate()에 이미 (당신이 NumberFormatException가 발생하고 fin 변수가 0로 끝나는 있도록 비어있는)을 EditText 내용을 분석하고 당신은 설정 부분에 도착하면 결과는 당신이 TextView (문제에 현재 0fin을 설정 (사용자가 Button 클릭)입니다 사용자가 클릭하면 Button 당신은 어떤 계산을 수행 할 현재EditText 콘텐츠를 결코하고 fin 변수는 오래 가치 (0)). onClick() 방법으로 계산 이동 :

Button solve = (Button)findViewById(R.id.getfinbutton); 
      solve.setOnClickListener(new OnClickListener() { 

       public void onClick(View v) { 
        double q1, q2, ex; 
     EditText etq1, etq2, eteg; 
     etq1 = (EditText)findViewById(R.id.editText1); 
      try{ 
       q1 = Double.parseDouble(etq1.getText().toString()); 
      } catch (NumberFormatException e) { 
       q1=0; 
      } 
     etq2 = (EditText)findViewById(R.id.editText2); 
      try{ 
       q2 = Double.parseDouble(etq2.getText().toString()); 
      } catch (NumberFormatException e){ 
       q2 = 0; 
      } 
     eteg = (EditText)findViewById(R.id.editText3); 
     try{ 
      ex = Double.parseDouble(eteg.getText().toString()); 
     } catch (NumberFormatException e){ 
      ex = 0; 
     } 
     fin = 0.4*q1+0.4*q2+0.2*ex; 
      if(fin == (int)fin){ 
       System.out.println((int)fin); 
      } 
      else{ 
       fin = 0.01*((int)(fin*100)); 
       System.out.println(fin); 
      } 
        TextView tvfin= TextView)findViewById(R.id.tvfinalgrade); 
         tvfin.setText(fin+""); 
       } 
      }); 
+0

굉장 했어! 94.6을 표시하는 대신 94.6000001을 표시합니다. 이 문제를 어떻게 해결할 수 있습니까? – Wilson

+0

@JesusSqueegee 이것을 시도해보십시오 :'tvfin.setText (String.format ("% .2f", fin));' – Luksprog

관련 문제