2012-12-18 143 views
-1

我試圖添加附加文本改變監聽到我的代碼,所以當 我對其進行編輯貨幣全自動轉換到選定的單選按鈕(美元,歐元),而不需要推動的一個文本單選按鈕,使其在textview中顯示答案。Android | AddTextChangedListener與單選按鈕

我不知道如何做到這一點 我是新來的節目在谷歌搜索沒有找到答案

package com.gardana.superh; 

public class ConvertActivity extends Activity { 
TextView mResult; 
EditText mToConvert; 
RadioGroup mRadioGroup; 
RadioButton mDollar, Meuro; 





protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_convert); 

    mResult = (TextView) findViewById(R.id.result); 
    mToConvert = (EditText) findViewById(R.id.toConvert); 
    mRadioGroup = (RadioGroup) findViewById(R.id.radioG); 
    mDollar = (RadioButton) findViewById(R.id.dollar); 
    Meuro = (RadioButton) findViewById(R.id.euro); 


    mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
    { 
     public void onCheckedChanged(RadioGroup mradRadioGroup, int checkedId) 
     { 

      switch (checkedId) 
      { 

      case R.id.dollar: 
      Double dollarConvert = Double.valueOf(mToConvert.getText().toString()); //convert the string to int 
      double price = dollarConvert * 3.76; 
      mDollar.setChecked(true); 
      Meuro.setChecked(false); 
      mResult.setText("$"+price); 

      break; 

      case R.id.euro: 

       Double euroConvert = Double.valueOf(mToConvert.getText().toString()); //convert the string to int 
       double value = euroConvert * 5; 
       mDollar.setChecked(false); 
       Meuro.setChecked(true); 

       mResult.setText("€" + value); 
       Meuro.setChecked(false); 

       break; 

       default:; 
      } 
     } 
    }); 



} 

}

回答

1

我建議你打破你的代碼,以使其更容易做。首先,您需要添加兩個偵聽器,以便在需要時執行轉換。

mToConvert.addTextChangedListener(new TextWatcher() { 

    @Override 
    public void afterTextChanged(Editable s) { 
     convertCurrentAmount(); 
    } 

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

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) {     
    } 


}); 
mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
    public void onCheckedChanged(RadioGroup mradRadioGroup, int checkedId) 
    { 
     convertCurrentAmount(); 
    } 
}); 

因爲你基本上會做同樣的工作在兩個地方,我建議你創建執行計算爲你的方法。然後,只要你喜歡更新轉換,就可以調用它。

public void convertCurrentAmount() { 
     int exchangeRate = -1; 
     String exchangeSymbol; 
     switch (mRadioGroup.getCheckedRadioButtonId()) { 
      case R.id.dollar: 
       exchangeRate = 3.76; 
       exchangeSymbol = "$"; 
       break; 

      case R.id.euro: 
       exchangeRate = 5; 
       exchangeSymbol = "€"; 
       break; 
     } 

     if (exchangeRate > 0 && exchangeSymbol != null) { 
      // Perform Calculation 
     } 
    } 

我還加入了一個建議,上述有關如何返工交換機/箱使用更少的代碼重複,因爲世界上沒有寫點同樣的事情兩次。

+0

還有一個問題,也許它有點新手問這一點,但爲什麼你的EXCHANGERATE設置爲-1而不是0?有什麼特別的理由 –

+1

的如果在底部條件查看是否EXCHANGERATE是執行計算之前大於0。通過初始化爲-1,您可以確保在未選中複選框的情況下不會進行計算。 – jimmithy