public void onClick(View v)
{
s.setText(s.getText().toString() + .5);
}
我希望當前在s(EditText)中的值高於之前單擊按鈕時的0.5,我是一個全新的java我不確定如何使這項工作。任何幫助是很好的幫助,提前謝謝。將EditText的當前值添加到設定值
public void onClick(View v)
{
s.setText(s.getText().toString() + .5);
}
我希望當前在s(EditText)中的值高於之前單擊按鈕時的0.5,我是一個全新的java我不確定如何使這項工作。任何幫助是很好的幫助,提前謝謝。將EditText的當前值添加到設定值
編輯:正如其他人所說,雙打通常比浮游物更好。
的getText()。toString()方法需要被轉化爲一個數值有可能添加的所有.5
try {
double currentValue = Double.parseDouble(s.getText().toString())
double newValue = currentValue + 0.5;
s.setText(newValue.toString());
} catch (NumberFormatException e) {
// text in EditText was not a parsable number
...
}
首先使用Double.parseDouble()
方法將字符串轉換爲加倍了。
double val=Double.parseDouble(s.getText().toString()) + 0.5;
s.setText(String.valueOf(val);
float會導致不需要的表示。所以如果你想要確切的表示,請嘗試BigDecimal
。
修改代碼
s.setText(new BigDecimal(s.getText().toString()).add(new BigDecimal("0.5")).toString());