键入EditText时刷新TextView

时间:2017-09-14 17:47:47

标签: android textwatcher

我正在构建一个结帐活动,用户在一个或多个EditText中输入值(Number)。在活动的底部,我需要使用所有EditText的总和来更新总值。

我试过像这样使用TextWatcher:

  @Override
        public void afterTextChanged(final Editable arg0) {
            if(!arg0.toString().equals(current)){
                        String bottom_value = total_value.getText().toString();
                        String number_total  = bottom_value.replaceAll("[^0-9]", "");//remove $
                        String value_arg = arg0.toString();
                        String number_arg  = value_arg.replaceAll("[^0-9]", "");//remove $

                        int final_int = Integer.parseInt(number_arg)+ Integer.parseInt(number_total);
                        total_value.setText(Integer.toString(final_int)); 
            }
        }

但问题是当用户输入的数字超过1时。如果输入1而不是2,则最终值必须为12,但我的代码为3(总和为1和2)。

对我的英语很抱歉,如果你不理解某些评论,我可以更好地解释。

HERE是我的应用的一个示例。

1 个答案:

答案 0 :(得分:3)

替换此

 int final_int = Integer.parseInt(number_arg)+ Integer.parseInt(number_total);

由此

 int final_int = Integer.parseInt(number_arg+number_total);