android TextWatcher beforeTextChanged问题

时间:2019-06-26 09:43:57

标签: android textview android-edittext android-textwatcher

在我的应用程序中,我有很多编辑文本,并且为他们实现了textWatcher,我希望它们增加和减少textView的数量,但我只能增加它

我试图检查旧文本是否比新文本大,并减少了textview的数量,但是每次都返回false

我的代码:

editText.addTextChangedListener(new TextWatcher() {

                            String oldText = "";

                            @Override
                            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                                this.oldText = s.toString();

                            }

                            @Override
                            public void onTextChanged(CharSequence s, int start, int before, int count) {


                            }

                            @Override
                            public void afterTextChanged(Editable s) {
                                if (!editText.getText().toString().equals("")) {
                                    int price = Integer.parseInt(editText.getText().toString()) * price_db;
                                    productPrice.setText(price + "");

                                    int totalPrice_n = Integer.parseInt(totalPrice.getText().toString());

                                    int min = Integer.parseInt(editText.getText().toString()) - Integer.parseInt(oldText);

                                    if(Integer.parseInt(editText.getText().toString()) > Integer.parseInt(oldText)){
                                        totalPrice.setText((totalPrice_n + min * price_db) + "");

                                    }else{
                                        totalPrice.setText((totalPrice_n - min * price_db) + "");
                                    }




                                }
                            }
                        });

我的问题是,如果条件仅返回false并转到其他部分,则我的EditText默认文本也设置为0,因此我认为beforeTextChanged仅采用0并检查新文本是否大于0

我用2个按钮(+,-)更改editText的文本,我想当我单击+按钮以增加TextView的数量,并且当我单击-按钮以减少TextView的数量时,但它只是增加它,我不知道为什么

1 个答案:

答案 0 :(得分:1)

将我的代码替换为您的代码并检查花花公子!!! :)

editText.addTextChangedListener(new TextWatcher() {

                        String oldText = "";

                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                            this.oldText = s.toString();

                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {


                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                            if (!editText.getText().toString().equalsIgnoreCase("")) 
            {
                int amount=Integer.parseI`enter code here`nt(editText.getText().toString());

                int price = Integer.parseInt(editText.getText().toString()) * price_db;

                int totalPrice_n = Integer.parseInt(totalPrice.getText().toString());

                productPrice.setText(price);                                    

                                int min = amount - Integer.parseInt(oldText);

                if(Integer.parseInt(oldText) > 0)
                {
                    if(amount > Integer.parseInt(oldText)){
                                        totalPrice.setText((totalPrice_n + min * price_db));
                                }else{
                                    totalPrice.setText((totalPrice_n - min * price_db));
                                }
                              }
                                else
                Toast.makeText(context,"old text is 0",Toast.LENGTH_LONG).show();
                            }
                        }
                    });