Android:处理多个 EditText,并行更新它们

Android: Handling multiple EditText, updating them parallely

你好我有 3 个编辑文本,当一些人在第一个编辑文本中输入值时,其他两个编辑文本将使用转换后的值进行更新。同样,当我在其他两个 edittexts 中输入值时,剩余的 edittexts 将被更新,但是由于它正在考虑并更新这些值,因此计算错误。如何关注 edittext 并仅考虑输入的值而不考虑其他计算值.

edt1.addTextChangedListener(new TextWatcher() {
    boolean editing = false;
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {   
    }

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

    @Override
    public void afterTextChanged(Editable s) {
        String qtyString = s.toString().trim();
        if(!editing) 
        { 
        editing = true;
        if(qtyString.length() > 0){
           edt2.setText(String.valueOf(hexTodecimal(qtyString)));
           edt3.setText(String.valueOf(hexTobinary(qtyString)));
        }
        else{
            edt2.setText("");
           edt3.setText("");
        }
        editing = false;
        } 

    }
});

我的编辑文本 2

edt2.addTextChangedListener(new TextWatcher() {
    boolean editing = false;
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {   
    }

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

    @Override
    public void afterTextChanged(Editable s) {
        String qtyString = s.toString().trim();
        if(!editing) 
        { 
        editing = true;
        if(qtyString.length() > 0){
            edt_hex_val.setText(String.valueOf(binarytohex(qtyString)));
           // edt_binary_val.setText(String.valueOf(hexTobinary(qtyString)));
        }
        else{
            edt_hex_val.setText("");
            //edt_binary_val.setText("");
        } 
        editing = false;
        } 

    }
});

我的编辑文本 3

edt3.addTextChangedListener(new TextWatcher() {
    boolean editing = false;
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {   
    }

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

    @Override
    public void afterTextChanged(Editable s) {
        String qtyString = s.toString().trim();
        if(!editing) 
        { 
        if(qtyString.length() > 0){
           // edt_decimal_val.setText(String.valueOf(hexTodecimal(qtyString)));
            edt_hex_val.setText(String.valueOf(binarytohex(qtyString)));
        }
        else{
            edt_hex_val.setText("");
            //edt_decimal_val.setText("");
        }
        editing = false;
        } 
    }
});
}

编辑:我试过如下但值没有更新

textWatcher=new TextWatcher() {

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

    } 

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

    } 

    @Override 
    public void afterTextChanged(Editable s) {
         qtyString = s.toString().trim();
        // System.out.println("printing sting==============="+qtyString);
          if (s == edt_hex_val.getEditableText()) {
            edt_decimal_val.removeTextChangedListener(textWatcher);
            edt_binary_val.removeTextChangedListener(textWatcher);


              if(qtyString.length() > 0 ){

                    edt_decimal_val.setText(String.valueOf(hexTodecimal(qtyString)));
                    edt_binary_val.setText(String.valueOf(hexTobinary(qtyString)));

                }else{

                     edt_decimal_val.setText("");
                     edt_binary_val.setText("");
                }


            } else if (s == edt_decimal_val.getEditableText()) {
                edt_hex_val.removeTextChangedListener(textWatcher);
                edt_binary_val.removeTextChangedListener(textWatcher);
                 if(qtyString.length() > 0){
                     edt_hex_val.setText(String.valueOf(hexTodecimal(qtyString)));
                     edt_binary_val.setText(String.valueOf(hexTobinary(qtyString)));
                 }else{

                    edt_hex_val.setText("");
                     edt_binary_val.setText("");
                }

            } else if (s == edt_binary_val.getEditableText()) {
                edt_hex_val.removeTextChangedListener(textWatcher);
                edt_decimal_val.removeTextChangedListener(textWatcher);
                 if(qtyString.length() > 0){
                     edt_hex_val.setText(String.valueOf(hexTodecimal(qtyString)));
                     edt_decimal_val.setText(String.valueOf(hexTobinary(qtyString)));
                 }else{

                    edt_hex_val.setText("");
                    edt_decimal_val.setText("");
                }

            } 

    } 
}; 

boolean editing 应该防止在设置其他 EditText-s 时递归触发 TextWatcher-events。但是,由于每个 TextWatcher 都有自己的 editing 实例,因此此保护不起作用。让 editing 更加全球化应该可以解决问题。

private EditText edt1;
private EditText edt2;
boolean editing = false; // all EditText-s use the same editing flag

edt1.addTextChangedListener(new TextWatcher() {
    // boolean editing = false; // make this global
    ....
});

edt2.addTextChangedListener(new TextWatcher() {
    // boolean editing = false; // make this global
    ....
});