How to get ID of multiple EditText dynamically add

2019-07-27 07:01发布

I've added multiple EditText and TextViews dynamically like:

final String[] meses = new String[]{"Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"};

    for(int i=0; i<meses.length; i++){

        LinearLayout layout = new LinearLayout(getContext());
        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        TextView textview = new TextView(getContext());
        ViewGroup.LayoutParams lparams = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
        textview.setLayoutParams(lparams);
        textview.setText(meses[i]);
        layout.addView(textview);

        EditText ed = new EditText(getContext());
        ed.setLayoutParams(lparams);
        ed.setId(i);

        layout.addView(ed);
        llMes.addView(layout);

    }

But now I need to retrieve the values of each EditText, how can I retrieve it, I have this:

for(int i=0; i < meses.length; i++){
                EditText et = null;
                et.getId();
                pago = quotas - Integer.parseInt(et.getText().toString());
            }

And also tried with findViewById(i) with for but also failed.

Thanks for helping me!

1条回答
混吃等死
2楼-- · 2019-07-27 07:24

Add EditTexts to an ArrayList then retrieve it by index.

//Arraylist that will contain EditTexts
ArrayList<EditText> list = new ArrayList<>();

        // for each EditText add it to the ArrayList
        for(int i=0; i < 10; i++){
            EditText et = new EditText();
            list.add(et);
        }

//To retrieve EditText
EditText et = list.get(0);
查看更多
登录 后发表回答