EditText更新/ onChange值添加侦听器

时间:2019-06-04 19:20:20

标签: java android

我的活动中有2个EditText,让我们说editText1editText2和一个双变量d=200

我想在用户更改/插入editText1的值时,editText2的值将实时更新为editText1*d

此外,当用户更改/插入editText2的值时,editText1将实时更新为editText2*d

我尝试使用addTextChangedListener->onTextChanged,但是当我同时为两个editText设置此功能,然后应用程序崩溃时,它对于一个Edit text来说效果很好 因为它造成了无限循环,我该如何解决这个问题?

update:bellow是我的代码

et1.addTextChangedListener(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) {

                            Editable a = et1.getText();
                            Double aa = Double.parseDouble(a.toString())*100;
                            String aaa = aa.toString();

                            et2.setText(aaa);

                        }

                        @Override
                        public void afterTextChanged(Editable s) {

                        }
                    });

                    et2.addTextChangedListener(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) {

                            Editable b=et2.getText();
                            Double bb=Double.parseDouble(b.toString());
                            String bbb=bb.toString();
                            et1.setText(bbb);


                        }

                        @Override
                        public void afterTextChanged(Editable s) {

                        }
                    });

3 个答案:

答案 0 :(得分:0)

您应该实现一个TextWatcher,它的工作原理与听起来相似。如果用户更改了EditText中现有的内容,它将“激活”此类。要对其进行测试,可以使用日志或在这些语句中的任何一条上设置断点。

private class MyTextWatcher implements TextWatcher {

    private View view;

    private MyTextWatcher(View view) {
        this.view = view;
    }

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

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
         //Your action here
          edit = fromEditText.getText().toString();

    }

    @Override
    public void afterTextChanged(Editable s) {
    }
}

onCreate中,添加一行以将您的EditText连接到TextWatcher

fromEditText.addTextChangedListener(new MainActivity.MyTextWatcher(fromEditText));

如果我没记错的话,可以将一堆EditText添加到一个TextWatcher中。您需要做的就是冲洗并重复。只需更改相应的EditText的名称并将其添加到TextWatcher

答案 1 :(得分:0)

我认为您必须先在textwatcher中创建TextWatcher_EdOne

 TextWatcher  TextWatcher_EdOne   = new TextWatcher() {

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    //disable editext2 textwatcher 
                    et2.removeTextChangedListener(TextWatcher_EdTwo);

                    }

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

                       ///do your stuff


                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                     // enable textwatcher on et2 again                                
                     et2.addTextChangedListener(TextWatcher_EdTwo);

                    }
                }

并将其用于其他textWatcher,这样您将避免无限循环

答案 2 :(得分:0)

EditText et1,et2;

TextWatcher watcher1 = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
        Editable a = et1.getText();
        Double aa = Double.parseDouble(a.toString())*100;
        String aaa = aa.toString();
        et2.removeTextChangedListener(watcher2);
        et2.setText(aaa);
        et2.addTextChangedListener(watcher2);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
};

TextWatcher watcher2 = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
        Editable b=et2.getText();
        Double bb=Double.parseDouble(b.toString());
        String bbb=bb.toString();
        et1.removeTextChangedListener(watcher1);
        et1.setText(bbb);
        et1.addTextChangedListener(watcher1);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }
};
et1.addTextChangedListener(watcher1);
et2.addTextChangedListener(watcher2);