如何在膨胀的视图中添加文本观察者



i m使用 LayoutInflater多次充气相同的视图。我想将TextWatcher设置为观察跨视图的文本更改。当我使用代码时,它不起作用。请帮助。

LayoutInflater inflater = LayoutInflater.from(UpdateUDISENext.this);
 View inflatedLayout = inflater.inflate(R.layout.udiseupdateview, null, false);
 headerLay.addView(inflatedLayout);
 ((TextView) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(0)).setText("class " + classvalue);
 totalmalestu = ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(0)).getChildAt(1));
 totalmalestu.setText(value);
 totalmalestu.setTextColor(Color.parseColor("#6bb40b"));
 totalfemalestu = ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(1)).getChildAt(1));
 totalfemalestu.setText(value2);
 totalfemalestu.setTextColor(Color.parseColor("#6bb40b"));
 totalstu = ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1));
 totalstu.setTextColor(Color.parseColor("#6bb40b"));
 ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1)).setText(String.valueOf(Integer.valueOf(value) + Integer.valueOf(value2)));
 totalmalestu.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }
    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
       if (totalmalestu.getText().toString().equals("")) {
                ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1)).setText(String.valueOf(0 + Integer.valueOf(totalfemalestu.getText().toString())));
       } else if (totalfemalestu.getText().toString().equals("")) {
               ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1)).setText(String.valueOf(Integer.valueOf(totalmalestu.getText().toString()) + 0));
       } else {
              String sum = String.valueOf(Integer.valueOf(totalmalestu.getText().toString()) + Integer.valueOf(totalfemalestu.getText().toString()));
              ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1)).setText(sum);
       }
    }
    @Override
    public void afterTextChanged(Editable editable) { }
   });

您可以使用单个文本观察者获得,然后用两个编辑文本注册。

TextWatcher textWatcher = new TextWatcher() {
    ... as you had it ...
};
totalmalestu.addTextChangedListener(textWatcher);
totalfemalestu.addTextChangedListener(textWatcher);

您在2个位置使用相同的条件

if (totalmalestu.getText().toString().equals("")) {
  ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1)).setText(String.valueOf(0 + Integer.valueOf(totalfemalestu.getText().toString())));
}else if (totalfemalestu.getText().toString().equals("")) {
   ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1)).setText(String.valueOf(Integer.valueOf(totalmalestu.getText().toString()) + 0));
}

第一个totalmalestu.getText().toString().equals("")

时,每次都会执行

所以问题是您的条件编辑它可以正常工作。

建议

您可以在 afterTextChanged(Editable editable)中编写代码,而不是ontextchanged(charsequence charsequence,int i,int i1,int i2)

@Override
public void afterTextChanged(Editable editable) {
    if (editable.toString().length()==0) {
       ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1)).setText(String.valueOf(0 + Integer.valueOf(totalfemalestu.getText().toString())));
    }else if (editable.toString().length()<2) {
         ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1)).setText(String.valueOf(Integer.valueOf(totalmalestu.getText().toString()) + 0));
    }else{
        String sum = String.valueOf(Integer.valueOf(totalmalestu.getText().toString()) + Integer.valueOf(totalfemalestu.getText().toString()));
        ((EditText) ((LinearLayout) ((LinearLayout) ((LinearLayout) headerLay.getChildAt(i)).getChildAt(1)).getChildAt(2)).getChildAt(1)).setText(sum);
     }
}

您可以根据需要更改状况...祝您好运...

最新更新