如何在android中将textListener设置为动态生成的editTexts

时间:2017-07-08 04:26:05

标签: java android

我已经动态添加了一个带有editBox的布局,我想将textListener设置为每个editBox,以便我可以对从editBox中检索的值执行一些计算。我正在使用下面的代码来扩充布局。是否可以在我用来膨胀布局的View对象上设置计数器。欢迎任何想法。

    public void addField1(View v) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View rowView = inflater.inflate(R.layout.field, null);
          parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
             }
    int count1 = parentLinearLayout.getChildCount();
                 for (int n = 0; n < count1; n++) {
                    LinearLayout linearLayout = (LinearLayout) parentLinearLayout.getChildAt(n);
        editValues3 = (EditText) linearLayout.getChildAt(3);
        editValues3.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                            Log.e("inside", "before" + s);

                        }
                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {
                            Log.e("inside", "on text" + s);  
                        }
                        @Override
                        public void afterTextChanged(Editable s) {
                            Log.e("inside", "afterText" + s);
                           if(editValues3.getText().length()>0){
                                valCalculations();
                            }
                        }
                    });
                }

1 个答案:

答案 0 :(得分:-1)

我认为您应该创建实现addTextChangedListener的CustomEditText,并在该自定义类中进行正确的计算。