2010年5月21日 星期五

refatcor你的程式吧!

refatcor你的程式吧!

話先說在前頭

如果我們開始寫程式之前沒有先好好的規劃一下的話!通常會隨著功能越加越多,程式越寫越亂!這時候我們就需要refactor一下程式囉!refactor中文翻成重構,其實也有點像是重新整理程式碼的感覺。不過每個人整理的習慣不太一樣,所以這邊我並沒有完全按照gasolin的程式碼做囉!

看到散落一地的 findViewsById 其實有點難過,整理的方式有兩種,一種是集中起來在一個procedure裡面,另一種就是寫成物件的屬性囉!
先來示範第一種方式


  1. 先在 onCreate() 的最下面新增 findViews(); 然後將滑鼠游標移到紅色的線上,這時候會跳出一個視窗,直接點[1 quick fix available: Create method 'findViews()']
  2. 這時候eclipes在下面就會新增一個 findViews() 的procedure。
  3. 然後接下來把所有的 findViewsById 搬過去,並宣告成變數就可以啦!
    1. 首先我們先把 Button button = (Button)findViewById(R.id.btnCalc); 搬過去。
    2. 搬過去之後,button.setOnClickListener(calcBMI); 下面會出現紅線,將滑鼠游標移到紅線上面,然後選擇第二個[Create field 'button'],這樣eclipse就會幫你建出 Button 的變數宣告了。
    3. 然後記得將 Button button = (Button)findViewById(R.id.btnCalc); 前面的 Button 拿掉。
    4. 重複1~3的動作,將所有宣告變數跟取得變數值得程式都搬移好吧。最後程式如下所示。

    1. package com.demo.android.bmi2;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.widget.Button;
    6. import android.widget.EditText;
    7. import android.widget.TextView;
    8. import android.view.View;
    9. import android.view.View.OnClickListener;
    10. import java.text.DecimalFormat;
    11.  
    12. public class Bmi2 extends Activity {
    13.    
    14.     private View btnCalc;
    15.     private TextView fieldHeight;
    16.     private TextView fieldWeight;
    17.     private TextView result;
    18.     private TextView fieldSuggest;
    19.  
    20.     /** Called when the activity is first created. */
    21.     @Override
    22.     public void onCreate(Bundle savedInstanceState) {
    23.         super.onCreate(savedInstanceState);
    24.         setContentView(R.layout.main);
    25.        
    26.         findViews();
    27.         setListeners();
    28.     }   
    29.    
    30.     private void setListeners() {
    31.         // TODO Auto-generated method stub
    32.         btnCalc.setOnClickListener(calcBMI);
    33.     }
    34.    
    35.     private void findViews() {
    36.         // TODO Auto-generated method stub
    37.         btnCalc = (Button)findViewById(R.id.btnCalc);
    38.         fieldHeight = (EditText)findViewById(R.id.etHeight);
    39.         fieldWeight = (EditText)findViewById(R.id.etWeight);
    40.         result = (TextView)findViewById(R.id.tvResult);
    41.         fieldSuggest = (TextView)findViewById(R.id.tvSuggest);
    42.     }
    43.  
    44.     private OnClickListener calcBMI = new OnClickListener()
    45.     {
    46.         public void onClick(View v)
    47.         {
    48.             DecimalFormat nf = new DecimalFormat("0.00");
    49.            
    50.             double height = Double.parseDouble(fieldHeight.getText().toString())/100;
    51.             double weight = Double.parseDouble(fieldWeight.getText().toString());
    52.             double bmi = weight / (height * height);
    53.            
    54.             result.setText("Your BMI is " + nf.format(bmi));
    55.  
    56.             if(bmi>25)
    57.             {
    58.                 fieldSuggest.setText(R.string.advice_heavy);
    59.             }
    60.             else if(bmi<20)
    61.             {
    62.                 fieldSuggest.setText(R.string.advice_light);
    63.             }
    64.             else
    65.             {
    66.                 fieldSuggest.setText(R.string.advice_avg);
    67.             }
    68.         }
    69.     };
    70. }

沒有留言:

張貼留言