-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

タイプ TextView のメソッド setText(CharSequence) は、引数 (double) には適用されません。

使用する

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)double を返す間、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 に答える
1

使用する:

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