2013-04-28 42 views
1

你們能否借給我一些新鮮的眼睛,告訴我在什麼地方我應該尋找找到缺失的鏈接?我正試圖通過從purchasePrice,downPayment和interestRate EditText字段中提取值來進行抵押計算,以進行計算。我通過將TextEdit字段設置爲利率來測試updateStandand()。但是,當我在AVD中運行應用程序時,如果我輸入購買價格,則每月付款字段將反映價格。如果我下降到利率領域並輸入數字,則每月支付字段將變爲利率值。更新錯誤的textedit字段

謝謝!

package com.example.mortgagecalc; 

import com.example.mortgagecalc.R.string; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.text.TextWatcher; 
import android.widget.EditText; 
import android.widget.SeekBar; 
import android.widget.SeekBar.OnSeekBarChangeListener; 
import android.widget.TextView; 
import android.text.Editable; 
public class Main extends Activity { 

    private static final String PURCHASE_AMOUNT = "PURCHASE_AMOUNT"; 
    private static final String CUSTOM_LENGTH = "CUSTOM_LENGTH"; 
    private static final String DOWN_PAYMENT = "DOWN_PAYMENT"; 
    private static final String INTEREST_RATE = "INTEREST_RATE"; 

    private int currentPurchaseAmount; //purchase amount entered by user 
    private int currentCustomLength; //length of loan set with SeekBar 
    private int currentDownPayment; //down payment entered by user 
    private double currentInterestRate; //interest rate entered by user 
    private EditText purchaseAmountEditText; //accepts user input for purchase amount 
    private EditText downPaymentEditText; //accepts user input for down payment amount 
    private EditText interestRateEditText; //accepts user input for interest rate 
    private EditText tenYearEditText; //display monthly payment for 10yr. loan 
    private EditText twentyYearEditText; //display monthly payment for 20yr. loan 
    private EditText thirtyYearEditText; //display monthly payment for 30yr. loan 
    private EditText customMonthlyEditText; //display monthly payment for custom length 
    private TextView customLengthTextView; //display custom loan length 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     //check if app just started or being restored from memory 
     if (savedInstanceState == null) //app just started running 
     { 
      currentPurchaseAmount = 0; //initialize to zero 
      currentCustomLength = 10; //initialize custom loan length to 10 
      currentDownPayment = 0; //initialize to zero 
      currentInterestRate = 0.0; //initialize to zero 
     } 
     else 
     { 
      currentPurchaseAmount = savedInstanceState.getInt(PURCHASE_AMOUNT); 
      currentCustomLength = savedInstanceState.getInt(CUSTOM_LENGTH); 
      currentDownPayment = savedInstanceState.getInt(DOWN_PAYMENT); 
      currentInterestRate = savedInstanceState.getDouble(INTEREST_RATE); 
     } 

     purchaseAmountEditText = (EditText) findViewById(R.id.purchaseAmountEditText); 
     downPaymentEditText = (EditText) findViewById(R.id.downPaymentEditText); 
     interestRateEditText = (EditText) findViewById(R.id.interestRateEditText); 
     tenYearEditText = (EditText) findViewById(R.id.tenYearEditText); 
     twentyYearEditText = (EditText) findViewById(R.id.twentyYearEditText); 
     thirtyYearEditText = (EditText) findViewById(R.id.thirtyYearEditText); 
     customMonthlyEditText = (EditText) findViewById(R.id.customMonthlyEditText); 
     customLengthTextView = (TextView) findViewById(R.id.customMonthlyTextView); 

     purchaseAmountEditText.addTextChangedListener(textWatcher); 
     downPaymentEditText.addTextChangedListener(textWatcher); 
     interestRateEditText.addTextChangedListener(textWatcher); 
     SeekBar customSeekBar = (SeekBar) findViewById(R.id.customSeekBar); 
     customSeekBar.setOnSeekBarChangeListener(customSeekBarListener); 
    } 

    private void updateStandard(){ 

     double interestRate = currentInterestRate; 
     //double loanPrinciple = currentPurchaseAmount - currentDownPayment; 

     //double tenYearMonthlyPayment = loanPrinciple; 
     tenYearEditText.setText(String.valueOf(interestRate)); 

    } 

    private void updateCustom(){ 

     //customLengthTextView.setText(currentCustomLength + "year"); 
     double customInterestRate = ((currentInterestRate/12) * .01); 
     int customLoanPrinciple = currentPurchaseAmount - currentDownPayment; 
     double customMonthlyPayment = customLoanPrinciple * customInterestRate/
             (1-Math.pow(1+customInterestRate, -(currentCustomLength*12))); 
     customMonthlyEditText.setText(Double.toString(customMonthlyPayment)); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState){ 
     super.onSaveInstanceState(outState); 

     outState.putDouble(PURCHASE_AMOUNT, currentPurchaseAmount); 
     outState.putDouble(DOWN_PAYMENT, currentDownPayment); 
     outState.putDouble(INTEREST_RATE, currentInterestRate); 
     outState.putDouble(CUSTOM_LENGTH, currentCustomLength); 
    } 

    private OnSeekBarChangeListener customSeekBarListener = new OnSeekBarChangeListener(){ 

     @Override 
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
     { 
      currentCustomLength = seekBar.getProgress(); 
      updateCustom(); 
     } 

     @Override 
     public void onStartTrackingTouch(SeekBar seekBar) 
     { 
     } 

     @Override 
     public void onStopTrackingTouch(SeekBar seekBar) 
     { 
     } 
    }; 

    private TextWatcher textWatcher = new TextWatcher() 
    { 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) 
     { 
      try 
      { 
       currentPurchaseAmount = Integer.parseInt(s.toString()); 
       currentDownPayment = Integer.parseInt(s.toString()); 
       currentInterestRate = Integer.parseInt(s.toString()); 
      } 
      catch(NumberFormatException e) 
      { 
       currentPurchaseAmount = 0; 
       currentDownPayment = 0; 
       currentInterestRate = 0.0; 
      } 

      updateStandard(); 
      updateCustom(); 
     } 

     @Override 
     public void afterTextChanged(Editable s) 
     { 
     } 

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

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

回答

1

對所有三個字段使用單個TextWatcher,並更新onTextChanged方法中的所有三個字段。

我猜你真正想要的是三個獨立的TextWatcher實例 - 每個字段一個,並且只更新每個值中的一個值。

+0

謝謝你的建議!我回過頭來看看TextWatcher並做了一些修改,看起來好像處於正常工作狀態。先生,再次感謝您! – user2318221 2013-04-28 22:11:10