使用TextWatcher的Android多个edittext

时间:2014-12-18 08:18:50

标签: android android-edittext textwatcher

我有三个edittexts,我使用Txtwatcher每个edittext,我无法获得以下功能 当我在ET1中聚焦输入值时,根据计算ET2 ET3将被更新,如果我将注意力集中在ET2上,  ET1和ET3将更新。

假设我在ET1中输入值(ET2和ET3将更新为值),现在如果将焦点更改为ET2,  ET2应擦除该值并应从第一个开始进入(也应擦除ET1和ET3值并替换为新值)

我做如下,但无法让它发挥作用。

edt_hex_val.addTextChangedListener(new TextWatcher() {
    @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(qtyString.length() > 0 && edt_hex_val.isFocused()){
            edt_decimal_val.setText(String.valueOf(hextodecimal(qtyString)));
            edt_binary_val.setText(String.valueOf(hexToBin(qtyString)));
        }
        else if( edt_hex_val.isFocused()){
            edt_decimal_val.setText("");
            edt_binary_val.setText("");
        }

    }
});

edt_decimal_val.addTextChangedListener(new TextWatcher() {
    @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(qtyString.length() > 0 && edt_decimal_val.isFocused()){
            edt_hex_val.setText(String.valueOf(hextodecimal(qtyString)));
            edt_binary_val.setText(String.valueOf(hexToBin(qtyString)));
        }
        else  if( edt_decimal_val.isFocused()){
            edt_hex_val.setText("");
            edt_binary_val.setText("");
        }

    }
});

edt_binary_val.addTextChangedListener(new TextWatcher() {
    @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(qtyString.length() > 0 && edt_binary_val.isFocused()){
            edt_decimal_val.setText(String.valueOf(hextodecimal(qtyString)));
            edt_hex_val.setText(String.valueOf(hexToBin(qtyString)));
        }
        else  if( edt_binary_val.isFocused()) {
            edt_hex_val.setText("");
            edt_binary_val.setText("");
        }

    }
});

2 个答案:

答案 0 :(得分:0)

试试这个......

edt_hex_val.addTextChangedListener(new TextWatcher() {
    @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(qtyString.length() > 0){
            edt_decimal_val.setText(String.valueOf(hextodecimal(qtyString)));
            edt_binary_val.setText(String.valueOf(hexToBin(qtyString)));
        }
        else{
            edt_decimal_val.setText("");
            edt_binary_val.setText("");
        }

    }
});

edt_decimal_val.addTextChangedListener(new TextWatcher() {
    @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(qtyString.length() > 0){
            edt_hex_val.setText(String.valueOf(hextodecimal(qtyString)));
            edt_binary_val.setText(String.valueOf(hexToBin(qtyString)));
        }
        else{
            edt_hex_val.setText("");
            edt_binary_val.setText("");
        }

    }
});

edt_binary_val.addTextChangedListener(new TextWatcher() {
    @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(qtyString.length() > 0){
            edt_decimal_val.setText(String.valueOf(hextodecimal(qtyString)));
            edt_hex_val.setText(String.valueOf(hexToBin(qtyString)));
        }
        else{
            edt_hex_val.setText("");
            edt_binary_val.setText("");
        }

    }
});

答案 1 :(得分:0)

您可以像这样编辑以解决堆栈溢出错误。

edt_binary_val.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_decimal_val.setText(String.valueOf(hextodecimal(qtyString)));
        edt_hex_val.setText(String.valueOf(hexToBin(qtyString)));
    }
    else{
        edt_hex_val.setText("");
        edt_binary_val.setText("");
    }

    editing = false;
   }


}
});