编辑:这是您建议的新代码:
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 each”循环的代码
http://www.leepoint.net/notes-java/flow/loops/foreach.html
然后当它全部总结时,它应该是变量结果中的一个双...然后我只需将文本设置为该数字......
我哪里出错?
答案 0 :(得分:3)
您可以在xml文件中创建2个布局,例如2个线性布局或任何您想要的布局。
在第一个布局中,单独添加Seekbar。
从Java代码获取id的第二个布局,并在每次进度更改时以编程方式将TextView添加/删除到第二个布局。
这可以按如下方式完成:
首先得到第二个布局
LinearLayout linearLayout = (LinearLayout)getActivity().findViewById(R.id.npv_calcfields);
第二步添加您的EditText:
EditText editText = new EditText(this);
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);
编辑:以下是一个有效的示例:
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if ( i > progress) {
i--;
EditText editText = (EditText) findViewById(i);
linearLayout.removeView(editText);
}
else {
EditText editText = new EditText(SeekBarActivity.this);
editText.setId(i);
editText.setHint("EditText No: " + (i+1));
editText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
linearLayout.addView(editText);
i++;
}
}
我希望我帮助过你。 ;)
答案 1 :(得分:0)
只需在xml中创建一个LinearLayout
,就像这个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/family_detail_linear"
android:orientation="vertical"
></LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/family_detail_addbutton"
android:text="Add More"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
并在LinearLayout
fragment
public class MyFrag extends Fragment{
private Button addmoremember;
private Activity activity;
private LinearLayout layout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.family_details, container, false);
activity=getActivity();
addmoremember=(Button)rootView.findViewById(R.id.family_detail_addbutton);
layout=(LinearLayout)rootView.findViewById(R.id.family_detail_linear);
addmoremember.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText edttexname= new EditText(activity);
edttexname.setHint("Enter Name");
layout.addView(edttexname);
}
});
return rootView;
}
}