0

在程序中,当我输入数据并按下启动按钮时,我有 3 个字段要输入,创建三个动态输入字段,程序仅计算 3 个顶级字段的平均分数。我希望当用户更改动态创建的字段中的任何数据时,它会计算这些字段中的数据,而不考虑它们的数量

 package com.example.averegemark;

    import android.app.Activity;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    import android.widget.TableRow.LayoutParams;
    import android.widget.TextView;
    import android.widget.Toast;

public class Table extends Activity {

    LinearLayout LL;
    EditText et1,et2,et3;
    double coef=0,points=0,sum=0,coefsum=0;
    TextView total;
    Button bNext;
    String name="",mark="",coeff="";
    int i=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.table);
        bNext = (Button) findViewById(R.id.bNext);
        et1 = (EditText) findViewById(R.id.et1);
        et2 = (EditText) findViewById(R.id.et2);
        et3 = (EditText) findViewById(R.id.et3);
        total = (TextView) findViewById(R.id.tvTotal);
        total.setText("GPA:");
        LL = (LinearLayout) findViewById(R.id.LL);

        bNext.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                name = et1.getText().toString();
                mark = et2.getText().toString();
                coeff = et3.getText().toString();
                if(mark.length()==0 || coeff.length()==0)
                    Toast.makeText(getApplicationContext(), "Enter mark `and coefficient", Toast.LENGTH_LONG).show();
                else
                    NewView(name,mark,coeff);
            }
        });

    }   

    private void NewView(String name, String mark, String coeff) {
        // TODO Auto-generated method stub
        EditText info1 = new EditText(this);
        EditText info2 = new EditText(this);
        EditText info3 = new EditText(this);

        info1.setId(i);i++;
        info1.setId(i);i++;
        info1.setId(i);i++;

        info1.setText(""+name);
        info2.setText(""+mark);
        info3.setText(""+coeff);


        TableLayout table = (TableLayout) findViewById(R.id.tbl);
        table.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        TableRow row = new TableRow(this);
        row.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        row.addView(info1);
        row.addView(info2);
        row.addView(info3);

        table.addView(row);


        String rez="";
        rez= info2.getText().toString();
        points = Double.parseDouble(rez);
        rez= info3.getText().toString();
        coef = Double.parseDouble(rez);
        sum +=coef*points;
        coefsum+=coef;
        sum/=coefsum;
        total.setText("GPA : "+sum+"\nCoefficient sum. "+coefsum);

        et3.setText("");
        et2.setText("");
        et1.setText("");
        et1.requestFocus();
    }
}

所以当我将更改行中的任何数据时,我想重新计算它,但在这里它只使用顶部 EditText 而不是动态创建:

            String rez="";
        rez= info2.getText().toString();
        points = Double.parseDouble(rez);
        rez= info3.getText().toString();
        coef = Double.parseDouble(rez);
        sum +=coef*points;
        coefsum+=coef;
        sum/=coefsum;
        total.setText("GPA : "+sum+"\nCoefficient sum. "+coefsum);
4

2 回答 2

0

当您按下按钮时,您会从原始编辑文本中获取信息。您必须找到表中的最后一行并从那里获取编辑文本。

于 2013-02-07T13:39:16.460 回答
0
CharSequence data = ((TextView) v).getText();
String result = data.toString();
于 2019-12-07T09:50:56.913 回答