0

编辑:这是您建议的新代码:

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

然后,当它全部加起来时,它应该是变量结果的两倍......然后我只需将文本设置为那个数字......

我哪里做错了?

4

2 回答 2

3

您可以在 xml 文件中创建 2 个布局,例如 2 个线性布局或任何您想要的布局。

在第一个布局中单独添加 Seekbar。

通过 id 从您的 Java 代码中获取第二个布局,并在每次进度更改时以编程方式将 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++;
    }
}

我希望我对你有所帮助。;)

于 2012-07-04T11:51:00.017 回答
0

只需LinearLayout在 xml 中创建一个 ..就像这样

<?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;
    }
}
于 2015-12-04T07:31:44.960 回答