编辑:这是你建议的新代码:编程的片段从搜索条创建的EditText领域
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
。
我的想法是
双R1用于在数学 TextView answer
用于输出最终的答案
EditText editText
是你告诉我,我需要引用它
现在,我想我将用户输入的所有值放入给定的字段中,然后对所有这些数据进行简单的数学运算。这是第一个for循环的用途。然后,当这个数组完成时,我想我会把它们加在一起,因为这是我想要的。我发现代码中的变量结果“对于每一个”循环这里
http://www.leepoint.net/notes-java/flow/loops/foreach.html
然后当它是所有概括起来应该是一个双...然后我会简单地设置文本到号码...
我哪里出错了?
的添加一些代码,会帮助,但我明白这我自己与开发商文档:)谢谢一分出来,这种解决方案似乎真的直观和好:) – Killerpixler
确定我不能弄明白,请大家帮忙,我更新它上面看到的,也是填充和东西不工作 – Killerpixler
@Killerpixler对不起,我迟到的回复:)我用一些代码更新了我的答案给你。 –