-1

我已经修改了我的活动以使用调用的自定义方法格式化 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();
    }


}
4

3 回答 3

1

利用:

result1.setText(String.ValueOf(round(mark1,2)));
result2.setText(String.ValueOf(round(mark2,2)));
于 2013-10-03T09:27:52.167 回答
1

TextView 类型中的方法 setText(CharSequence) 不适用于参数(双精度)

利用

double r1 = round(mark1,2);
result1.setText(String.valueOf(r1)); 
double r2 = round(mark2,2);
result1.setText(String.valueOf(r2)); 

setText(CharSequence)round(mark1,2)当您返回双精度时,将 CharSequence 作为参数 。

Java 文档

public static String valueOf(double d)
Returns the string representation of the double argument.
The representation is exactly the one returned by the Double.toString method of one argument.

Parameters:
d - a double.
Returns:
a string representation of the double argument.
See Also:
Double.toString(double)
于 2013-10-03T09:28:09.953 回答
1
// try this
 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);

        result1.setText(round(mark1,2));
        result2.setText(round(mark2,2));
    }

    public static String 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 String.valueOf(bd.doubleValue());
    }
于 2013-10-03T09:30:35.907 回答