tChangedListener作品一个的EditText但不为

tChangedListener作品一个的EditText但不为

本文介绍了addTextChangedListener作品一个的EditText但不为其他的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问过解决它的百般会后这个问题。

我有两个EditText上定义如下:

 私人的EditText firstText;
私人的EditText secondText;

我重置1的EditText上的其他变化,反之亦然值。
我得到的,因为同样的原因,调用的EditText递归的StackOverflow上的错误。

因此​​,我也跟着this联系解决我的问题

幸运的是,一切工作,但只针对secondText不使用firstText。这意味着,当我为secondText改变数值,我看到firstText更新值。但同样也不去firstText。

当我通过打印garbbish值试图调试我知道firstText不调用addTextChangedListener。

下面是code为我的EditText

  firstTextTW =新TextWatcher(){
        @覆盖
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数后INT){        }        @覆盖
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
            的System.out.println(我是里面firstTextTW);
            。INT afterCount = firstText.getText()长();
            的System.out.println(afterCount);
            如果(afterCount == 1){
                //如果字符为null,则什么也不做            }其他{
                的System.out.println(内部firstTextTw); //通过印刷调试
                双数= Double.parseDouble(s.toString());
                。字符串SetCurrency = actv.getText()的toString();
                。字符串SecondCurrency = actv2.getText()的toString();                SetCurrency = SetCurrency.trim();
                SecondCurrency = SecondCurrency.trim();                SecondTask任务=新SecondTask();
                尝试{
                    双CurrencyDiff;
                    CurrencyDiff = Double.parseDouble(task.execute(http://api.fixer.io/latest?base=+​​ SetCurrency +与&符号=+ SetCurrency +,+ SecondCurrency)获得());                    双final_result =数量* CurrencyDiff;
                    双final_value = Math.round(final_result * 1000.0)/ 1000.0;
                    secondText.removeTextChangedListener(secondTextTW);
                    secondText.setText(final_value.toString());
                }赶上(InterruptedException的E){
                    e.printStackTrace();
                }赶上(为ExecutionException E){
                    e.printStackTrace();
                }
            }
        }        @覆盖
        公共无效afterTextChanged(编辑S){        }
    };    secondTextTW =新TextWatcher(){
        @覆盖
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数后INT){        }        @覆盖
        公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
            双number_to_be_divided_from = Double.parseDouble(s.toString());
            。字符串自动1 = actv.getText()的toString();
            。字符串自动2 = actv2.getText()的toString();            AUTO1 = auto1.trim();
            AUTO2 = auto2.trim();            SecondTask任务=新SecondTask();
            双currency_converter;            尝试{
                currency_converter = Double.parseDouble(task.execute(http://api.fixer.io/latest?base=+​​ AUTO1 +与&符号=+ AUTO1 +,+ AUTO2)获得());
                的System.out.println(currency_converter);
                双resultToUpdate = number_to_be_divided_from / currency_converter;
                双resultToUpdateFinal = Math.round(resultToUpdate * 1000.0)/ 1000.0;
                //System.out.println(resultToUpdateFinal);
                firstText.removeTextChangedListener(firstTextTW);
                firstText.setText(resultToUpdateFinal.toString());
            }赶上(InterruptedException的E){
                e.printStackTrace();
            }赶上(为ExecutionException E){
                e.printStackTrace();
            }
        }        @覆盖
        公共无效afterTextChanged(编辑S){        }
    };    firstText.addTextChangedListener(firstTextTW);
    secondText.addTextChangedListener(secondTextTW);

正如人们可以看到我同时在打电话的EditText和addTextChangedListener我甚至firstTextTW定义和secondTextTW正确的全局变量只是下面的两个的EditText

在此先感谢

编辑 - code为用于NUM键盘setOntouchListener

  firstText.setOnTouchListener(新View.OnTouchListener(){        @覆盖
        公共布尔onTouch(视图V,MotionEvent事件){
            v.onTouchEvent(事件);
            InputMethodManager IMM =(InputMethodManager)v.getContext()getSystemService(Context.INPUT_METHOD_SERVICE)。
            如果(IMM!= NULL){
                imm.hideSoftInputFromWindow(v.getWindowToken(),0);                nine.setOnClickListener(新View.OnClickListener(){
                    @覆盖
                    公共无效的onClick(视图v){
                        firstText.setText(firstText.getText()插入(firstText.getText()长(),9));
                    }
                });                eight.setOnClickListener(新View.OnClickListener(){
                    @覆盖
                    公共无效的onClick(视图v){
                        firstText.setText(firstText.getText()插入(firstText.getText()长度(),8));
                    }
                });                seven.setOnClickListener(新View.OnClickListener(){
                    @覆盖
                    公共无效的onClick(视图v){
                        firstText.setText(firstText.getText()插入(firstText.getText()长(),7));
                    }
                });                six.setOnClickListener(新View.OnClickListener(){
                    @覆盖
                    公共无效的onClick(视图v){
                        firstText.setText(firstText.getText()插入(firstText.getText()长度(),6)。);
                    }
                });                five.setOnClickListener(新View.OnClickListener(){
                    @覆盖
                    公共无效的onClick(视图v){
                        firstText.setText(firstText.getText()插入(firstText.getText()长(),5));
                    }
                });                four.setOnClickListener(新View.OnClickListener(){
                    @覆盖
                    公共无效的onClick(视图v){
                        firstText.setText(firstText.getText()插入(firstText.getText()长度(),4));
                    }
                });                three.setOnClickListener(新View.OnClickListener(){
                    @覆盖
                    公共无效的onClick(视图v){
                        firstText.setText(firstText.getText()插入(firstText.getText()长度(),3));
                    }
                });                two.setOnClickListener(新View.OnClickListener(){
                    @覆盖
                    公共无效的onClick(视图v){
                        firstText.setText(firstText.getText()插入(firstText.getText()长度(),2));
                    }
                });                one.setOnClickListener(新View.OnClickListener(){
                    @覆盖
                    公共无效的onClick(视图v){
                        firstText.setText(firstText.getText()插入(firstText.getText()长度(),1));
                    }
                });                zero.setOnClickListener(新View.OnClickListener(){
                    @覆盖
                    公共无效的onClick(视图v){
                        。INT text_count = firstText.getText()长();
                        如果(text_count == 1){
                        }其他{
                            firstText.setText(firstText.getText()插入(firstText.getText()长度(),0));
                        }
                    }
                });
                dot.setOnClickListener(新View.OnClickListener(){
                    @覆盖
                    公共无效的onClick(视图v){
                        firstText.setText(firstText.getText()插入(firstText.getText()长())。。);
                    }
                });                delete.setOnClickListener(新View.OnClickListener(){
                    @覆盖
                    公共无效的onClick(视图v){
                        诠释计数= firstText.getText()长()。
                        如果(计数== 1)
                        {
                            firstText.setText();
                            的System.out.println(替换空白文本);
                        }
                        其他{
                            firstText.setText(。firstText.getText()删除(firstText.getText()长() - 1,firstText.getText()长()));
                        }
                    }
                });
            }
            返回true;
        }
    });


解决方案

您做到像

  mSearchbox.addTextChangedListener(新TextWatcher(){
        @覆盖
        公共无效onTextChanged(CharSequence的CS,诠释开始,诠释之前,诠释计数){
            ivCancelSearch.setVisibility(mSearchbox.getText()长度()大于0 View.VISIBLE:View.GONE?);
        }        @覆盖
        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数后INT){
        }        @覆盖
        公共无效afterTextChanged(编辑S){        }
    });

similer其他记住得到的EditText的正确的ID

I am asking this question after going through every possible way of resolving it.

I have two EditText defined as follows

private EditText firstText;
private EditText secondText;

I am resetting the value of one EditText on change of the other and vice versa.I was getting a stackOverFlow error because of the very same reason of calling EditText recursively.

Hence, I followed this link to solve my problem

Luckily, things work but only for secondText not for the firstText. This means when I change value for secondText, I see updated value in firstText. But same does not go for firstText.

When I tried debugging by printing a garbbish value I got to know that firstText is not calling on addTextChangedListener.

Here is the code for both of my EditText

 firstTextTW = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            System.out.println("I am inside firstTextTW");
            int afterCount = firstText.getText().length();
            System.out.println(afterCount);
            if(afterCount == 1) {
                //if the character is null then do nothing

            }else{
                System.out.println("Inside firstTextTw");//debugging by printing
                Double number = Double.parseDouble(s.toString());
                String SetCurrency = actv.getText().toString();
                String SecondCurrency = actv2.getText().toString();

                SetCurrency = SetCurrency.trim();
                SecondCurrency = SecondCurrency.trim();

                SecondTask task = new SecondTask();
                try {
                    double CurrencyDiff;
                    CurrencyDiff = Double.parseDouble(task.execute("http://api.fixer.io/latest?base=" + SetCurrency + "&symbols=" + SetCurrency + "," + SecondCurrency).get());

                    Double final_result = number * CurrencyDiff;
                    Double final_value = Math.round(final_result * 1000.0) / 1000.0;
                    secondText.removeTextChangedListener(secondTextTW);
                    secondText.setText(final_value.toString());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    secondTextTW = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            Double number_to_be_divided_from = Double.parseDouble(s.toString());
            String auto1 = actv.getText().toString();
            String auto2 = actv2.getText().toString();

            auto1 = auto1.trim();
            auto2 = auto2.trim();

            SecondTask task = new SecondTask();
            Double currency_converter;

            try {
                currency_converter = Double.parseDouble(task.execute("http://api.fixer.io/latest?base=" + auto1 + "&symbols=" + auto1 + "," + auto2).get());
                System.out.println(currency_converter);
                Double resultToUpdate = number_to_be_divided_from / currency_converter;
                Double resultToUpdateFinal = Math.round(resultToUpdate * 1000.0) / 1000.0;
                //System.out.println(resultToUpdateFinal);
                firstText.removeTextChangedListener(firstTextTW);
                firstText.setText(resultToUpdateFinal.toString());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    firstText.addTextChangedListener(firstTextTW) ;
    secondText.addTextChangedListener(secondTextTW);

As one can see I am calling addTextChangedListener on both the EditText and I have even defined firstTextTW and secondTextTW properly as global variables just below the two EditText

Thanks in advance

Edit- code for setOntouchListener used for num keypad

firstText.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            v.onTouchEvent(event);
            InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

                nine.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        firstText.setText(firstText.getText().insert(firstText.getText().length(), "9"));
                    }
                });

                eight.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        firstText.setText(firstText.getText().insert(firstText.getText().length(), "8"));
                    }
                });

                seven.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        firstText.setText(firstText.getText().insert(firstText.getText().length(), "7"));
                    }
                });

                six.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        firstText.setText(firstText.getText().insert(firstText.getText().length(), "6"));
                    }
                });

                five.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        firstText.setText(firstText.getText().insert(firstText.getText().length(), "5"));
                    }
                });

                four.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        firstText.setText(firstText.getText().insert(firstText.getText().length(), "4"));
                    }
                });

                three.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        firstText.setText(firstText.getText().insert(firstText.getText().length(), "3"));
                    }
                });

                two.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        firstText.setText(firstText.getText().insert(firstText.getText().length(), "2"));
                    }
                });

                one.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        firstText.setText(firstText.getText().insert(firstText.getText().length(), "1"));
                    }
                });

                zero.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int text_count = firstText.getText().length();
                        if(text_count == 1) {
                        }else{
                            firstText.setText(firstText.getText().insert(firstText.getText().length(), "0"));
                        }
                    }
                });


                dot.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        firstText.setText(firstText.getText().insert(firstText.getText().length(), "."));
                    }
                });

                delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int count = firstText.getText().length();
                        if(count==1)
                        {
                            firstText.setText(" ");
                            System.out.println("Replace text with blank space");
                        }
                        else {
                            firstText.setText(firstText.getText().delete(firstText.getText().length() - 1, firstText.getText().length()));
                        }
                    }
                });
            }


            return true;
        }
    });
解决方案

You do as like

    mSearchbox.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence cs, int start, int before, int count) {
            ivCancelSearch.setVisibility(mSearchbox.getText().length() > 0 ? View.VISIBLE : View.GONE);
        }

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

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

similer for other remember get correct id of edittext

这篇关于addTextChangedListener作品一个的EditText但不为其他的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:37