android从另一个中减去一个输入

时间:2012-04-10 15:40:17

标签: android textview user-input

我有2个对象

obj.targetWeight = targetWeight.getText().toString();
obj.currentWeight = currentWeight.getText().toString();

我想从另一个中减去一个。

我试过这个没有运气。

if (obj != null){
    outputEditText.setText("Your target weight is " + obj.targetWeight + 
                            "\n Your current weight is " + obj.currentWeight + "\n" 
                        + (obj.currentWeight - obj.targetWeight));
}

我很确定这不是应该做的,但我无法弄清楚如何去做。

2 个答案:

答案 0 :(得分:3)

您必须先将它们转换为整数。

if (obj != null) {
    int targetWeight = Integer.parseInt(obj.targetWeight.getText().toString());
    int currentWeight = Integer.parseInt(obj.currentWeight.getText().toString());
    outputEditText.setText(
        "Your target weight is " + obj.targetWeight + "\n" + 
        "Your current weight is " + obj.currentWeight + "\n" +
        "Difference is " + (currentWeight - targetWeight));
    }
}

答案 1 :(得分:0)

谢谢kailoon,我有一个答案。

if (obj != null) {
        int targetWeight = Integer.parseInt(obj.targetWeight);
        int currentWeight = Integer.parseInt(obj.currentWeight);
        outputEditText.setText(
            "Your target weight is " + obj.targetWeight + "\n" + 
            "Your current weight is " + obj.currentWeight + "\n" +
            "Difference is " + (targetWeight - currentWeight ));
        }