-1
我有2个edittext字段为空,我想当我在两个字段中键入数字第三个textview会自动得到2上面edittexts的划分。 所以没有按钮来计算,等更新textview没有任何按钮
事情是当我在任何领域的应用程序获取强制关闭输入一个数字。这里是我的代码和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);
}
感谢您的回复人
其bcoz你试图比较一个不是有效整数的空字符串。 –
是的事情是,它只要输入一个数字就能捕获并拦截我的程序。那全是0。 – lantonis