从动态创建的EditText中获取文本

时间:2013-02-07 13:05:35

标签: java android android-edittext dynamically-generated

在程序中我输入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);

2 个答案:

答案 0 :(得分:0)

按下按钮时,您将从原始edittexts获取信息。你必须找到表格中的最后一行并从那里获取edittexts。

答案 1 :(得分:0)

CharSequence data = ((TextView) v).getText();
String result = data.toString();