从字符串的EditText浮动 [英] String from EditText to float

查看:93
本文介绍了从字符串的EditText浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对将要使用的浮点数一个EditText。所以我想读取来自的EditText文本,并把它变成一个浮动的变量。但是,我似乎有一个文本浮动的问题。这是该行我有:

I have an EditText for which will be using for a float number. So I'm trying to read the text from the EditText and put it into a float variable. But I seem to have a text to float problem. This is the line I have:

float Number = ( ( EditText )findViewById( R.id.edit_float ) ).getText();

我已经使用Float.parseFloat(串),只是一般的铸造尝试过,但似乎没有做到这一点。我能做些什么吗?另外,有没有办法将其写入到一个变量之前,检查是否有有效的浮点数?

I've tried using Float.parseFloat(string) and just general casting, but nothing seem to do it. What can I do here? Also, is there a way to check for a valid float number before writing it to a variable?

推荐答案

试试这个。

EditText edt = (EditText) findViewById(R.id.edit_float);
float number = Float.valueOf(edt.getText().toString());

您使用的valueOf()方法,如果浮动包装类将字符串转换为float。在这个例子中,我得到的是,编辑对象的EditText 的getText()对我称之为的toString()办法从中获得一个字符串。

You use the valueOf() method if the Float wrapper class to convert a string to a float. IN this example I get the Editable object of that EditText with getText() on which I call the toString() method to obtain a string from it.

更新:完全正确的家伙遗憾。时间递增我的羊计数器。

Update: Totally right guys sorry. Time to increment my sheep counter.

这篇关于从字符串的EditText浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆