你们可以借给我一些新鲜的眼睛,然后告诉我在哪里找到缺失的链接?我试图通过从purchasePrice,downPayment和interestRate EditText字段中提取值来进行抵押计算来进行计算。我通过将TextEdit字段设置为利率来测试updateStandand()。但是,当我在AVD中运行应用程序时,如果我键入购买价格,则每月付款字段将反映价格。如果我向下移动到利率字段并输入数字,则每月付款字段将更改为利率值。
谢谢!
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;
}
}
答案 0 :(得分:1)
您正在为所有三个字段使用单个TextWatcher,并更新onTextChanged方法中的所有三个字段。
我猜你真正想要的是三个单独的TextWatcher实例 - 每个字段一个,并且每个字段只更新一个值。