android TextWatcher beforeTextChanged problem



在我的应用程序中,我有许多编辑文本,我为它们实现了文本观看者,我希望它们增加并减少文本视图号码,但我只能增加它

我试图检查旧文本是否更大,并降低文本视图号码,但每次都会返回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并转到其他一部分,我的deDittext默认文本将设置为0,所以我认为BeforeTextChanged仅在0上占0,并检查新文本是否大于0

我更改使用2按钮( , - (的EditText文本,当我单击 按钮以增加文本视图号码以及我单击 - 按钮以减少文本视图号码时,我想要

用您的代码替换我的代码,然后检查花花公子!!!:(

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();
                            }
                        }
                    });

相关内容

  • 没有找到相关文章

最新更新