我已经修改了我的活动以使用调用的自定义方法格式化 EditText 输出,round
但是当我调用将结果设置为 EditTexts 的方法时,我收到错误:
The method setText(CharSequence) in the type TextView is not applicable for the arguments (double)
。我从中收集到添加额外参数时存在问题2
使用时,settext
但我不确定如何输出结果。我的问题是在这种情况下,使用此round
功能时我将如何输出结果?
public class CalcResult extends MainActivity
{
EditText result1,result2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
result1 = (EditText)findViewById(R.id.mark1);
result2 = (EditText)findViewById(R.id.mark2);
Intent intent = getIntent();
double mark1 = intent.getDoubleExtra("number1", 0);
double mark2 = intent.getDoubleExtra("number2", 0);
//set the variables on EditTexts like this :
result1 = (EditText)findViewById(R.id.mark1);
result2 = (EditText)findViewById(R.id.mark2);
result1.setText(round(mark1,2));
result2.setText(round(mark2,2));
}
public static double round(double value, int places) {
if (places < 0)
throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, BigDecimal.ROUND_HALF_UP);
return bd.doubleValue();
}
}