2012-10-15 76 views
0

我有一個應用程序與幾個EditTexts。用戶留空,程序計算空變量。這是啓動它的按鈕的onClickListener。我期望在交換機之前完成所有的解析工作,這樣我只需要解析一次所有的條目,然後將0放入空的一個,因爲這會產生錯誤。使解析更容易在android EditText

如何簡化此代碼?

public void buttonCalc() { 
    calc.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      pv = (EditText) getActivity().findViewById(R.id.pv_pv); 
      fv = (EditText) getActivity().findViewById(R.id.pv_fv); 
      r = (EditText) getActivity().findViewById(R.id.pv_r); 
      n = (EditText) getActivity().findViewById(R.id.pv_n); 
      t = (EditText) getActivity().findViewById(R.id.pv_t); 
      answer = (TextView) getActivity().findViewById(R.id.pv_answer); 
      //check for fields and choice 
      if (pv.getText().toString().equals("")) {emptyfields++;choice = 1;} 
      if (fv.getText().toString().equals("")) {emptyfields++;choice = 2;} 
      if (r.getText().toString().equals("")) {emptyfields++;choice = 3;} 
      if (n.getText().toString().equals("")) {emptyfields++;choice = 4;} 
      if (t.getText().toString().equals("")) {emptyfields++;choice = 5;} 
      if (emptyfields > 1) {MiscMethods.ErrorToast(1);} 
      else{ 
       emptyfields--; 
       try { 

       } catch (Exception e) { 
       } 
       switch (choice) { 
       case 1:// pv 
        double fv1 = Double.parseDouble(fv.getText().toString()); 
        double r1 = Double.parseDouble(r.getText().toString()); 
        double n1 = Double.parseDouble(n.getText().toString()); 
        double t1 = Double.parseDouble(t.getText().toString()); 
        double result1 = fv1/(Math.pow(1+(r1/n1),n1*t1)); 
        result1 = MiscMethods.rounder(result1, 2); 
        answer.setText(answer_pv + result1); 
        break; 
       case 2:// fv 
        double pv2 = Double.parseDouble(pv.getText().toString()); 
        double r2 = Double.parseDouble(r.getText().toString()); 
        double n2 = Double.parseDouble(n.getText().toString()); 
        double t2 = Double.parseDouble(t.getText().toString()); 
        double result2 = pv2*(Math.pow(1 + (r2/n2), n2 * t2)); 
        result2 = MiscMethods.rounder(result2, 2); 
        answer.setText(answer_fv + result2); 
        break; 
       case 3:// r 
        double pv3 = Double.parseDouble(pv.getText().toString()); 
        double fv3 = Double.parseDouble(fv.getText().toString()); 
        double n3 = Double.parseDouble(n.getText().toString()); 
        double t3 = Double.parseDouble(t.getText().toString()); 
        double result3 = ((Math.pow(fv3/pv3, (1/(n3 * t3)))) - 1)* n3; 
        result3 = MiscMethods.rounder(result3, 4); 
        answer.setText(answer_r +" "+ result3 + "/" + (result3*100)+"%"); 
        break; 
       case 4:// n 
        MiscMethods.ErrorToast(2); 
        break; 
       case 5:// t 
        double pv5 = Double.parseDouble(pv.getText().toString()); 
        double fv5 = Double.parseDouble(fv.getText().toString()); 
        double n5 = Double.parseDouble(n.getText().toString()); 
        double r5 = Double.parseDouble(r.getText().toString()); 
        double result5 = (Math.log((fv5/pv5)))/((Math.log((1 + (r5/n5)))) * n5); 
        result5 = MiscMethods.rounder(result5, 2); 
        answer.setText(answer_t +" "+ result5); 
        break; 
       default: 
        MiscMethods.ErrorToast(3); 
        break; 
       }// switch ends 
      }// else ends 

     } 

    }); 
} 

回答

2

你可以做的是這樣的:

//initialise doubles with default 0 value when string is empty 
double pvDbl = getDouble(pv); 
double fvDbl = getDouble(fv); 
//etc 

getDouble方法可能是這樣的:

private static double getDouble(EditText et) { 
    if(et.getText().length() == 0) { 
     return 0; 
    } 
    try { 
     return Double.parseDouble(et.getText().toString()); 
    } catch (NumberFormatException e) { 
     return 0; //default value for invalid entries? 
        //Or maybe show an error message? 
    } 
} 

然後,您可以取代你switchif聲明:

if (pvDbl == 0) { 
    //formula for pv null 
} else if (fvDbl == 0) { 
    //formula for fv null 
} //etc. 

請注意,您還需要處理0條目(在我建議的代碼中,不能在空條目""和條目"0"之間做出區別。

+0

+1爲了讓我輸入幾乎相同:-) –

+1

在我的回答中,只有額外的評論是爲每個編輯文本添加一個onTextChangeListener,以便您可以檢查這些條目是否在用戶添加時解析爲double。 –

+0

感謝這兩者,並且我喜歡ChangeListener的想法......用紅色圖層圍繞字段,或者當用戶在BS中鍵入時感謝... :)謝謝 – Killerpixler