How to get ID of multiple EditText dynamically added?

别等时光非礼了梦想. 提交于 2020-01-24 19:56:06

问题


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:


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);


来源:https://stackoverflow.com/questions/45017297/how-to-get-id-of-multiple-edittext-dynamically-added

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!