Android并行更新编辑文本

时间:2014-12-10 10:59:04

标签: android android-edittext textwatcher

在我的活动中,我有三个EditText字段,当我在EditText要更新的其他两个EditText字段的任何一个中输入值时,假设{{1} AI输入值4,EditText B应该有十进制值,EditText C应该有二进制值。我正在尝试使用文本观察者,但它在某种程度上不起作用,有人能指出我正确的方向我如何并行更新EditText

EditText

方法:

edditvalue.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();
        hexToBin(qtyString);

    }
});

2 个答案:

答案 0 :(得分:0)

代码变化不大......

  edditvalue.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
              String qtyString = s.toString().trim();
                hexToBin(qtyString);

            }

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


        }

        @Override
        public void afterTextChanged(Editable s) {


        }
    });

        } 

答案 1 :(得分:0)

我尝试了您的代码并添加了setText次调用。它在EditTexts中的打印值正确。

editText1.addTextChangedListener(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) {
        // TODO Auto-generated method stub
        String qtyString = s.toString().trim();
        //you would need to handle your code in this manner so app doesn't crash if user hits backspace and removes all entered characters
        if(qtyString.length() > 0){
            editText2.setText(hexToBin(qtyString));
            editText3.setText(String.valueOf(hextodecimal(qtyString)));
        }
        else {
            editText2.setText("");
            editText3.setText("");
        }
    }
});

希望这有帮助。

P.S:确保正确使用inputType,否则如果用户在editText1中输入字符,您的应用就会崩溃