无需任何按钮即可更新textview

时间:2013-01-22 11:25:09

标签: android textview

我有2个edittext字段为空,我想当我在两个字段中键入数字时,第3个textview将自动获得上述2个edittexts的分区。 所以没有按钮来计算等等。

问题是,当我在任何字段上输入一个数字时,应用程序将被强制关闭。这是我的代码和logcat:

    amount.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable arg0) {
        }

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

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            AddFuelActivity.this.updateValue();
        }

    });

    litres.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable arg0) {
        }

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

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            AddFuelActivity.this.updateValue();
        }

    });


protected void updateValue() {
    int amountInt = Integer.parseInt(amount.getText().toString());
    int litresInt = Integer.parseInt(litres.getText().toString());
    float priceFuelAuto = 0;
    priceFuelAuto = amountInt / litresInt;
    fuelPrice.setText("€ " + priceFuelAuto);
}

logcat错误:我认为这些是重要的错误。

01-22 13:23:21.650: E/AndroidRuntime(671): java.lang.NumberFormatException: Invalid int: ""
01-22 13:23:21.650: E/AndroidRuntime(671):  at java.lang.Integer.invalidInt(Integer.java:138)
01-22 13:23:21.650: E/AndroidRuntime(671):  at java.lang.Integer.parseInt(Integer.java:359)
01-22 13:23:21.650: E/AndroidRuntime(671):  at java.lang.Integer.parseInt(Integer.java:332)
01-22 13:23:21.650: E/AndroidRuntime(671):  atcom.example.mycarfuel.AddFuelActivity.updateValue(AddFuelActivity.java:155)
01-22 13:23:21.650: E/AndroidRuntime(671):  atcom.example.mycarfuel.AddFuelActivity$2.onTextChanged(AddFuelActivity.java:67)

我修复了我的代码。这是我做的:

    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        if (litres.length() > 0) {
            AddFuelActivity.this.updateValue();
        }
    }

    int amountInt = 0;
    int litresInt = 0;
    try {
        amountInt = Integer.parseInt(amount.getText().toString());
        litresInt = Integer.parseInt(litres.getText().toString());
    } catch (NumberFormatException e) {
        litresInt = 0;
        amountInt = 0;
    }
    if(amountInt !=0 && litresInt != 0){
        float priceFuelAuto = 0;
        priceFuelAuto = amountInt / litresInt;
        fuelPrice.setText("€ " + priceFuelAuto);
    }

感谢您的回复人员

2 个答案:

答案 0 :(得分:0)

@Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            if(edittext1.length()!=0){
            updateValue() 

            }
            else{
                edittext2.setText("");
            }

答案 1 :(得分:0)

只需写下代码..

 amount.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable arg0) {
        }

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

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
             if(s.length()>0)     <<<<===Just put this condition
            AddFuelActivity.this.updateValue();
        }

    });

    litres.addTextChangedListener(new TextWatcher() {

        public void afterTextChanged(Editable arg0) {
        }

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

        public void onTextChanged(CharSequence s, int start, int before,
                int count) 
        {
             if(s.length()>0)     <<<<===Just put this condition
            AddFuelActivity.this.updateValue();
        }

    });


protected void updateValue() {
    int amountInt = Integer.parseInt(amount.getText().toString());
    int litresInt = Integer.parseInt(litres.getText().toString());
    float priceFuelAuto = 0;
    priceFuelAuto = amountInt / litresInt;
    fuelPrice.setText("€ " + priceFuelAuto);
}