如何获取动态添加的多个EditText的ID? [英] How to get ID of multiple EditText dynamically added?

查看:101
本文介绍了如何获取动态添加的多个EditText的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态添加了多个EditText和TextView,例如:

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

    }

但是现在我需要检索每个EditText的值,我该如何获取它:

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

并且还尝试将 findViewById(i)用于,但是也失败了.

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

感谢您的帮助!

推荐答案

将EditTexts添加到ArrayList中,然后按索引进行检索.

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

这篇关于如何获取动态添加的多个EditText的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆