简体   繁体   中英

Error formatting EditText output

I have modified my activity to format the EditText output using a custom method called round but when I call the method on setting the results to the EditTexts I get the error: The method setText(CharSequence) in the type TextView is not applicable for the arguments (double) .I have gathered from this that there is a problem with adding the extra parameter of 2 when using settext but I'm not sure how else to output the results.My question is how in this case would I output the results when using this round function?

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();
    }


}

Use:

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

The method setText(CharSequence) in the type TextView is not applicable for the arguments (double)

Use

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

setText(CharSequence) takes a CharSequence as a param while you have a round(mark1,2) returning double.

java docs

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)
// 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());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM