我有一个将用于浮点数的 EditText。所以我试图从 EditText 中读取文本并将其放入浮点变量中。但是我好像有个文字要浮动的问题。这是我的路线:
float Number = ( ( EditText )findViewById( R.id.edit_float ) ).getText();
我试过使用 Float.parseFloat(string) 和一般的转换,但似乎什么也没做。我可以在这里做什么?另外,有没有办法在将其写入变量之前检查有效的浮点数?
原文由 Espen 发布,翻译遵循 CC BY-SA 4.0 许可协议
尝试这个。
如果
Float
包装类将字符串转换为浮点数,则使用valueOf()
方法。 IN this example I get theEditable
object of thatEditText
withgetText()
on which I call thetoString()
method to obtain a string from it .更新: 完全正确的家伙抱歉。是时候增加我的绵羊计数器了。