1

我有一个Button添加一行EditText到一个TableLayout。我需要找出EditText添加的每一行中的输入。

例如:某人添加了 3 行,表示有 3 行EditText。在每个EditText中,他们输入数字 3。我需要存储EditText添加到布局中的值,并通过单击按钮将它们全部添加。

我会使用一个列表然后迭代该列表吗?我不确定我将如何做到这一点。

到目前为止,这是我的代码...

int count = 1;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);       
    setContentView(R.layout.activity_main);
    getSavedState();

    Button buttonAdd = (Button) findViewById(R.id.button1);
    Button buttonDel = (Button) findViewById(R.id.button2);
    Button buttonCalc = (Button) findViewById(R.id.button3);
    buttonAdd.setOnClickListener(this);
    buttonDel.setOnClickListener(this);
}

public void onClick(View v) {
    TableLayout tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1);
    List<EditText> allEd = new ArrayList<EditText>();

    switch(v.getId()){
        case R.id.button1:
            if(count != 16){
                count++;

                // Create the row only when the add button is clicked
                TableRow tempRow = new TableRow(MainActivity.this);
                EditText tempText1 = new EditText(MainActivity.this);
                EditText tempText2 = new EditText(MainActivity.this);
                TextView tempTextView = new TextView(MainActivity.this);
                EditText editText1 = (EditText) findViewById(R.id.editText1);
                EditText editText2 = (EditText) findViewById(R.id.editText2);
                TextView textView3 = (TextView) findViewById(R.id.textView3);
                tempTextView.setText(count + ".");

                tempRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
                tempText1.setLayoutParams(editText1.getLayoutParams());
                tempText2.setLayoutParams(editText2.getLayoutParams());
                tempTextView.setLayoutParams(textView3.getLayoutParams());
                tempText1.setInputType(InputType.TYPE_CLASS_TEXT);
                tempText2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                tempText2.setId(count);
                allEd.add(tempText2);

                tempRow.addView(tempTextView);
                tempRow.addView(tempText1);
                tempRow.addView(tempText2);
                tableLayout1.addView(tempRow);
            } 
            else {
                final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
                alertDialog.setTitle("Error");
                alertDialog.setMessage("You can only have 10 rows!");

                alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                    }
                });

                alertDialog.show();
            }
            break;
        case R.id.button2:
            if(count != 1){
                count--;
                tableLayout1.removeView(tableLayout1.getChildAt(count));
        } 
            else {
                final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
                alertDialog.setTitle("Error");
                alertDialog.setMessage("You must have at least one row!");

                alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                    }
                });

                alertDialog.show();
            }
            break;
        case R.id.button3:
            String[] strings = new String[allEd.size()];
            for(int i = 0; i < allEd.size(); i++) {
                strings[i] = allEd.get(i).getText().toString();
                int input = Integer.parseInt(strings[i]);

                final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); //Read Update
                alertDialog.setTitle("Your calculated GPA");
                alertDialog.setMessage("Your calculated GPA is: " + input/count);
                alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                    }
                });

                alertDialog.show();
            }
        break;

    }
4

2 回答 2

2

您需要保留对EditText每次按下按钮时添加的 s 的引用。由于你不知道会添加多少,我建议使用 aList<EditText>来存储它们。将其设为成员变量,然后您可以从EditText列表中的 s 访问输入。

于 2012-10-31T01:05:04.797 回答
1

当您创建每个EditTexts 时,您需要将它们添加到类似数组的容器中,例如ArrayList<EditText>. 每当您需要访问这些动态EditTexts 中的任何一个时,您都可以从ArrayList.

我可以看到您已经ArrayList<EditText>在您的onClick()方法中创建了一个。这很好,但是您需要将方法移到ArrayList外部,onClick()以便可以从您创建的任何其他方法中引用它。所以移动这条线......

List<EditText> allEd = new ArrayList<EditText>();

在方法之外,所以现在的开头几行onClick()看起来像这样......

List<EditText> allEd = new ArrayList<EditText>();

public void onClick(View v) {
    TableLayout tableLayout1 = (TableLayout) findViewById(R.id.tableLayout1);

    switch(v.getId()){
    case R.id.button1:

另外,请确保您将所有EditTexts 添加到ArrayList- 我看到有一行allEd.add(tempText2);但我看不到任何行allEd.add(tempText1);- 如果您需要访问tempText1,请确保将其添加到列表中。

要执行“加法”计算,您需要遍历 中的条目ArrayList,获取它们的值,然后将它们相加。像这样的东西...

case R.id.button3:
    // calculate the value first
    int calculation = 0;
    for (int i=0;i<allEd.size();i++){
        // get the entry
        EditText textField = addEd.get(i);

        try {
            // get the value, as a number
            int numberValue = Integer.parseInt(textField.getText());
            // add it to the calculation
            calculation += numberValue;
        } 
        catch (Exception e){
            // ignore errors if the input is empty or isn't a number
        }
    }

    // now display the result
    final AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
    alertDialog.setTitle("Your calculated GPA");
    alertDialog.setMessage("Your calculated GPA is: " + input/count);
    alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            alertDialog.dismiss();
        }
    });

    alertDialog.show();
break;
于 2012-10-31T01:22:30.640 回答