2013-05-01 21 views
1

我正在開發Android應用程序。一切似乎工作正常,但我想使我的代碼比現在更短。你可能得到的想法,很多線路是重複的:Java:如何縮短經常使用的代碼?

public void ButtonKlick (View view) { 


    double zahl1; 
    double zahl2; 
    double zahl3; 
    double zahl4; 
    double Ergebnis = 0; 
    EditText Feld1 = (EditText)findViewById(R.id.zahl1); 
    EditText Feld2 = (EditText)findViewById(R.id.zahl2); 
    EditText Feld3 = (EditText)findViewById(R.id.zahl3); 
    EditText Feld4 = (EditText)findViewById(R.id.zahl4); 
    EditText FeldErgebnis = (EditText)findViewById(R.id.etErgebnis); 
    if (Feld1.getText().toString().length() == 0) { 
     return; 
    } 
    if (Feld2.getText().toString().length() == 0) { 
     return; 
    } 
    if (Feld3.getText().toString().length() == 0) { 
     return; 
    } 
    if (Feld4.getText().toString().length() == 0) { 
     return; 
    } 
    zahl1 = Double.parseDouble(Feld1.getText().toString()); 
    zahl2 = Double.parseDouble(Feld2.getText().toString()); 
    zahl3 = Double.parseDouble(Feld3.getText().toString()); 
    zahl4 = Double.parseDouble(Feld4.getText().toString()); 


    Ergebnis = Math.sqrt(Math.pow(zahl4 - zahl3, 2) + Math.pow(zahl2 - zahl1, 2)); 

    FeldErgebnis.setText(String.valueOf(Ergebnis)); 
} 

這兩種方法都開始從:

double zahl1; 

zahl4 = Double.parseDouble(Feld4.getText().toString()); 

有沒有得到任何具體的方式擺脫那些相同的路線?

+0

你應該嘗試並使用MVC設計模式:使View類具有公共getter setter方法來獲取/設置編輯文本的值。然後,讓Activity類使用View類中的方法來獲取值,進行計算並顯示顯示。 – 2013-05-01 13:43:30

+0

你檢查了我的答案嗎,或者我不明白你想要什麼? – codeMagic 2013-05-01 14:24:09

回答

1

是。代替每個Button具有不同的功能,只需使用一個,然後在您的xml中獲取id of the按鈕. I assume you are declaring the onClick`。只是聲明相同的功能,如果這些都做同樣的事情

public void ButtonKlick (View view) { 

switch (view.getId()) 
{ 
    case (R.id.button1Id): 
    // do specific stuff for button with id button1Id like 
    Ergebnis = Math.sqrt(Math.pow(zahl4 - zahl3, 2) + Math.pow(zahl2 - zahl1, 2)); 
    break; 
    case (R.id.button2Id): 
    Ergebnis = (zahl4 - zahl3)/(zahl2 - zahl1); 
    break; 
    ... 
} 
double zahl1; 
double zahl2; 
double zahl3; 
double zahl4; 
double Ergebnis = 0; 
EditText Feld1 = (EditText)findViewById(R.id.zahl1); 
EditText Feld2 = (EditText)findViewById(R.id.zahl2); 
EditText Feld3 = (EditText)findViewById(R.id.zahl3); 
EditText Feld4 = (EditText)findViewById(R.id.zahl4); 
EditText FeldErgebnis = (EditText)findViewById(R.id.etErgebnis); 
if (Feld1.getText().toString().length() == 0) { 
    return; 
} 
if (Feld2.getText().toString().length() == 0) { 
    return; 
} 
if (Feld3.getText().toString().length() == 0) { 
    return; 
} 
if (Feld4.getText().toString().length() == 0) { 
    return; 
} 
zahl1 = Double.parseDouble(Feld1.getText().toString()); 
zahl2 = Double.parseDouble(Feld2.getText().toString()); 
zahl3 = Double.parseDouble(Feld3.getText().toString()); 
zahl4 = Double.parseDouble(Feld4.getText().toString()); 


Ergebnis = Math.sqrt(Math.pow(zahl4 - zahl3, 2) + Math.pow(zahl2 - zahl1, 2)); 

FeldErgebnis.setText(String.valueOf(Ergebnis)); 

}

如果我知道你想那麼這將繼續從需要不同的方法做同樣的事情什麼

+0

我在你的代碼中發現了一個問題。你必須在計算部分後面聲明變量。但總的來說,這對我來說非常有用!我只需要在同一個方法中聲明所有的按鈕,然後詢問按鈕ID!謝謝:)祝你有美好的一天 – 2013-05-01 14:34:06

+0

啊,我明白你在說什麼了。我複製並粘貼,然後將相關更改放在頂部,以便識別。很高興能幫到您 – codeMagic 2013-05-01 14:41:33

+0

是的,它有幫助! :) – 2013-05-01 15:15:29

2

寫這樣一個實用的方法:

private double getDouble(EditText tv) { 
    return Double.parseDouble(tv.getText().toString()); 
} 

,並調用它像:

zahl4 = getDouble(Field4); 

編輯:

private double getDouble(int viewId) { 
    View view = findViewById(viewId); 
    double toReturn = 0; 
    // instanceOf returns false for null values 
    if (view instanceOf EditText) 
     toReturn = Double.parseDouble(view.getText().toString()); 
    return toReturn; 
} 
+0

只是爲了得到額外的懶惰,是否需要EditText ID並在方法中找到EditText(前提是此方法對於活動是本地的,並且您可以使用findViewById) – dymmeh 2013-05-01 13:28:45

+0

@dymmeh我不明白你的意思。看我的編輯 – Blackbelt 2013-05-01 13:33:05

+0

我想你錯過了我。但是,謝謝。無論如何不幫助我。非常感謝您的幫助。 :) – 2013-05-01 14:13:15