【问题标题】:How to add text watcher on inflated view如何在膨胀视图上添加文本观察器
【发布时间】:2017-09-13 07:41:00
【问题描述】:

我正在使用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) { }

   });

【问题讨论】:

  • addTextChangedListener 不工作吗?你怎么知道的?
  • 我有超过 1 个相同类型的膨胀视图。我想在相同的膨胀视图中显示变化。请参考我附加的图像。
  • 我没有看到任何图像.. 究竟是什么不工作? addTextChangedListener ?
  • 我的布局有两个用于男性和女性计数的编辑文本。还有一个是总数。我希望如果我更改男性或女性编辑文本,那么它会总计添加和结果集。所以我对男性和女性使用了文本观察器。但由于不止一个相同类型的膨胀布局,它无法正常工作。
  • 现在看上面的图片描述

标签: android android-layout android-textwatcher


【解决方案1】:

您可以使用一个文本观察器,然后注册两个编辑文本。

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

totalmalestu.addTextChangedListener(textWatcher);
totalfemalestu.addTextChangedListener(textWatcher);

【讨论】:

  • 我不止一次夸大了同一个视图。所以在我的布局中,我有不止一个malestu 和femalestu。如何区分它们。表示系统如何知道该布局的哪个男性调用了文本更改侦听器。
【解决方案2】:

你在 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);
     }
}

你可以随心所欲地改变你的状态......祝你好运......

【讨论】:

    猜你喜欢
    • 2016-09-29
    • 2020-03-15
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多