将编辑文本转换为双重android

som*_*smc 4 double int android android-edittext

我有2个编辑文本,因为我想让它们成为小数,我不能使它们成为整数,所以我必须将它们变成双精度.我有这个代码,但在n2Var获取文本我有一个错误.

    mul = (Button) findViewById(R.id.button);
    n1 = (EditText)findViewById(R.id.editText);
    n2 = (EditText)findViewById(R.id.editText2);
    ans = (EditText)findViewById(R.id.TextView10);

    double n1Var = Double.parseDouble(n1.getText().toString());
    double n2Var = Double.parseDouble(n2,getText().toString());
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助!

Ind*_*ile 10

你有这个在n2Var

 double n2Var = Double.parseDouble(n2,getText().toString());
Run Code Online (Sandbox Code Playgroud)

代替

 double n2Var = Double.parseDouble(n2.getText().toString());
Run Code Online (Sandbox Code Playgroud)


小智 6

在 Kotlin 中你可以使用这个..

edittext.text.toString().toDouble()
Run Code Online (Sandbox Code Playgroud)