2012-08-03 37 views
0

我剛開始學習Java/Android編程。這是我的第一個基本程序,現在工作正常。感謝堆溢出人幫助我。現在,如果你能幫助我調整這個程序,這將會非常有幫助,它會讓我更好地理解Android編程的基本結構。下面是代碼...(簡介:這將攝氏轉換爲華氏,反之亦然,你鍵入..)如何簡化此Android程序?

package mag.com.myhelloworldapp; 

import android.os.Bundle; 
import android.app.Activity; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.View; 
import android.view.View.OnFocusChangeListener; 
import android.widget.EditText; 


public class MainActivity extends Activity { 

    private EditText celsiusText; 
    private EditText farenheitText; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     celsiusText = (EditText) findViewById(R.id.editText1); 
     farenheitText = (EditText) findViewById(R.id.editText2); 
     celsiusText.setOnFocusChangeListener(new OnFocusChangeListener() { 

      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       ((EditText)findViewById(R.id.editText2)).setText(""); 
       ((EditText)findViewById(R.id.editText1)).setText(""); 
      } 
     }); 

     farenheitText.setOnFocusChangeListener(new OnFocusChangeListener() { 

      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       ((EditText)findViewById(R.id.editText2)).setText(""); 
       ((EditText)findViewById(R.id.editText1)).setText(""); 
      } 
     }); 


     farenheitText.addTextChangedListener(new TextWatcher() 
     { 
      @Override 
      public void onTextChanged(CharSequence S, int start, int before, int count) 
      { 
       if(farenheitText.hasFocus()) 
       { 
       float inputValue; 
       if (!S.toString().equals("")) 
       { 
        inputValue = Float.parseFloat(S.toString()); 
          ((EditText)findViewById(R.id.editText1)).setText(String 
            .valueOf(convertFahrenheitToCelsius(inputValue))); 

       } 
       else 
       { 
        ((EditText)findViewById(R.id.editText1)).setText(""); 
        return; 
       } 
       } 
      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) 
      { 

      } 

      @Override 
      public void afterTextChanged(Editable s) 
      { 

      } 
     }); 


     celsiusText.addTextChangedListener(new TextWatcher() 
     { 

         public void afterTextChanged(Editable s) { 

        } 

        public void beforeTextChanged(CharSequence s, int start, 
        int count, int after) { 
        } 
       public void onTextChanged(CharSequence S,int start,int before, int count){ 
        if (celsiusText.hasFocus()) 
        { 
        float inputValue; 

        if (!S.toString().equals("")) 
        { 
         inputValue = Float.parseFloat(S.toString()); 
        ((EditText)findViewById(R.id.editText2)).setText(String 
           .valueOf(convertCelsiusToFahrenheit(inputValue))); 

        } 
        else 
        { 
         ((EditText)findViewById(R.id.editText2)).setText(""); 
         return; 
        } 
          } 
       } 
       });  


    } 



//Converts to celsius 
     private float convertFahrenheitToCelsius(float fahrenheit) { 
     return ((fahrenheit - 32) * 5/9); 
     } 

     // Converts to fahrenheit 
     private float convertCelsiusToFahrenheit(float celsius) { 
     return ((celsius * 9)/5) + 32; 
     } 


    } 

這又是activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" > 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="128dp" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="62dp" 
     android:ems="10" 
     android:inputType="numberSigned" > 

     <requestFocus /> 
    </EditText> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="128dp" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/editText1" 
     android:layout_alignBottom="@+id/editText1" 
     android:layout_alignParentRight="true" 
     android:ems="10" 
     android:inputType="numberSigned" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/editText1" 
     android:layout_below="@+id/editText1" 
     android:layout_marginTop="18dp" 
     android:text="@string/celsius" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/textView1" 
     android:layout_alignBottom="@+id/textView1" 
     android:layout_alignRight="@+id/editText2" 
     android:text="@string/fahrenheit" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 

感謝您的幫助。 。

+0

爲什麼修改,如果它的正常工作。改爲寫另一個。 – 2012-08-03 19:11:44

+0

由於該應用運行良好,你只需要幫助簡化它。這個問題可能更適合[CodeReview(Beta)](http://codereview.stackexchange.com/)。 – Sam 2012-08-03 19:16:50

+0

你是對的亞歷克斯,但我只想了解我所做的所有基本錯誤。這次自檢將幫助我下次更好地編寫代碼。 – Mahesh 2012-08-03 19:18:15

回答

0

相反的:

((EditText)findViewById(R.id.editText2)).setText(""); 
((EditText)findViewById(R.id.editText1)).setText(""); 

你應該簡單地重用你已經有這樣的一個:

celsiusText.setText(""); 
farenheitText.setText(""); 

上找到查看每次當你已經分配的對象是沒有意義的。

當然,這對所有在你的代碼中使用地方:

((EditText)findViewById(R.id.editText1)) 
((EditText)findViewById(R.id.editText2)) 
+0

我已經更新了代碼。感謝您的輸入.. – Mahesh 2012-08-06 08:56:26

0

我現在看到的唯一的事情:

一旦存儲在XML變量的意見,你不需要再尋找他們(findViewById)。只需使用您創建的變量。

+0

非常感謝答案Lxx。我將更改第一個建議的代碼。雖然我對第二個問題有疑問,但檢查條件.hasfocus的原因是,如果我編輯celsiustext中的文本,華氏會自動更改,結果它會因堆棧溢出錯誤而失敗。爲了克服這個問題,我正在檢查改變的文本框是否有焦點,然後繼續並更新其他編輯文本。我的理解是否正確? – Mahesh 2012-08-03 19:40:57

+0

你是對的。我會刪除點2. – Ixx 2012-08-03 19:48:59