简体   繁体   English

我怎么知道,有多少个EditText用户添加?

[英]How can I know, how many EditText user add?

I inflate an EditText and I setId(i++) | 我给EditText充气,然后setId(i++) | static int i = 0;

and this is the code : 这是代码:

 buttonAdd.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {




            LayoutInflater layoutInflater =
                    (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View addOne = layoutInflater.inflate(R.layout.row, null);
            final EditText textOut = (EditText)addOne.findViewById(R.id.textout);
            Button buttonRemove = (Button)addOne.findViewById(R.id.remove);
            buttonRemove.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    ((LinearLayout) addOne.getParent()).removeView(addOne);


                }
            });

            container.addView(addOne);
                textOut.setId(i++);  }});

so, if I add an EditText, the Id will be 0 and if I add another EditText, the Id will be 1 .. etc 因此,如果我添加一个EditText,则ID为0 ;如果添加另一个EditText,则ID为1 ..等等

so, the user can select how many EditText he want, right ? 因此,用户可以选择他想要多少个EditText,对吗?

if the user add 3 EditText for example 例如,如果用户添加3 EditText

How can my code knows ? 我的代码怎么知道?

for example, if I want sum all the values of EditText, and the user add 3 EditTixt 例如,如果我想对所有EditText的值求和,然后用户添加3 EditTixt

I will do this 我会做的

( EditText one = (EditText)findViewById(0) ) + ( EditText dodo  =  (EditText)findViewById(1) ) + ( EditText dodo = (EditText)findViewById(2) )

But, if I don't know how many EditTixt the user will add ! 但是,如果我不知道用户将添加多少个EditTixt! what can I do ? 我能做什么 ?

thank you. 谢谢。

int count = 0;
int childcount = container.getChildCount();
for (int i=0; i < childcount; i++){
      View v = container.getChildAt(i);
      if(view.getTag.equals("edittext"){
       count ++;
     }

}

and change this line in your code 并在代码中更改此行

final View addOne = layoutInflater.inflate(R.layout.row, null);
  addOne.setTag("edittext");

count return you total edittext added by user 计数返回用户添加的总edittext

take look on this code it will count edit text for you in your layout container 看一下这段代码,它将为您的布局容器中的编辑文本计数

first you need to making changed in your code like this and it worked with me fine : 首先,您需要像这样对代码进行更改,它可以很好地与我一起工作:

 final ViewGroup addOne = (ViewGroup) layoutInflater.inflate(R.layout.test, null);

int count = 0 ; 
    for (int i = 0; i < addOne .getChildCount(); i++) {

        if(addOne .getChildAt(i).getClass().getSimpleName().equals("EditText"))
            count ++ ;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM