编辑:这是您建议的新代码:
package com.mwerner.mycalc.finance;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class F_NPV extends Fragment {
EditText[] DynamicField = new EditText[16];
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.npv, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final LinearLayout linearLayout = (LinearLayout)getActivity().findViewById(R.id.npv_calcfields);
EditText editText = new EditText(getActivity());
final int i = 0;
editText.setId(i); //Set id so that you can remove that EditText in the future.
editText.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
linearLayout.addView(editText);
SeekBar bar = (SeekBar) getActivity().findViewById(R.id.npv_seekbar);
final TextView selection = (TextView) getActivity().findViewById(R.id.npv_selected);
bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekbar, int progress,
boolean fromUser) {
// to display to the user how many he has selcted
selection.setText("You chose " + progress + "periods");
if ( i > progress) {
i--;
EditText editText = (EditText) getActivity().findViewById(i);
linearLayout.removeView(editText);
}
else {
EditText editText = new EditText(getActivity());
editText.setId(i);
editText.setHint("EditText No: " + (i+1));
editText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
linearLayout.addView(editText);
i++;
}
}
public void onStopTrackingTouch(SeekBar seekbar) {}
public void onStartTrackingTouch(SeekBar arg0) {}
});
}
}
编辑2:
当我快速向下滑动时,我修复了减少字段数量的“滞后”。
bar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekbar, int progress,
boolean fromUser) {
// to display to the user how many he has selcted
selection.setText("You chose " + progress + "periods");
if ( i > progress) {
while (i > progress) {
i--;
EditText editText = (EditText) getActivity().findViewById(i);
linearLayout.removeView(editText);
}
}
else {
while (i < progress) {
EditText editText = new EditText(getActivity());
editText.setId(i);
editText.setHint("EditText No: " + (i+1));
editText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
linearLayout.addView(editText);
i++;
}
}
}
public void onStopTrackingTouch(SeekBar seekbar) {}
public void onStartTrackingTouch(SeekBar arg0) {}
});
现在我需要对每个字段进行相同的数学运算:
answer = entry / (Math.pow(1+r , i)
基本上每个条目都除以(1+r)
该字段具有的 id 的幂...所以第一个文本编辑除以(1+r)^0
,第二个字段除以(1+r)^1
等等..我该怎么做?
编辑3:
calc.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Double r1 = Double.parseDouble(r.getText().toString());
EditText editText = (EditText) getActivity().findViewById(i);
TextView answer = (TextView) getActivity().findViewById(R.id.npv_answer);
double[] CashFlows;
CashFlows = new double[i];
double result = 0;
for (int i1 = 0; i1 < i; i1++) {
CashFlows[i] = (Double.parseDouble(editText.getText().toString()))/(Math.pow(1+r1, i));
}
for(double d : CashFlows) {
result += d;
}
answer.setText("answer is " + result);
}
});
基本上这是我的思考过程。请告诉我哪里出了严重的错误,因为这会在fragment
加载时导致崩溃。
我的想法是
double r1 用于数学运算
TextView answer
,用于输出最终答案
EditText editText
是你告诉我的,我需要参考它
现在我想我会将用户输入给定字段的所有值放入一个数组中,并对所有这些值进行简单的数学运算。这就是第一个 for 循环的用途。然后我当那个数组完成后,我想我会把它们加在一起,因为这就是我想要的。我在这里找到了“For each”循环的代码
http://www.leepoint.net/notes-java/flow/loops/foreach.html
然后,当它全部加起来时,它应该是变量结果的两倍......然后我只需将文本设置为那个数字......
我哪里做错了?