English 中文(简体)
从EditText到浮游
原标题:String from EditText to float
  • 时间:2010-11-19 21:50:10
  •  标签:
  • java
  • android

我有一个EditText,用于一个浮标。 因此,我试图从EditText读到案文,把它变成一个浮动变量。 但是,我似乎有一个案文存在问题。 这是我拥有的一条线:

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

我先尝试使用Float.parseFloat(扼制)和只是一般的投放,但似乎没有这样做。 我可以做些什么? 另外,在将有效浮标编号写到变数之前,是否还有办法加以核对?

最佳回答

引证。

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

如果<代码>Float 包装类别将浮体转换为浮体,则请使用 ValueOf()。 一、导言 Editable Object of that EditText with getText(<>/code> on which I calls the toString( methods to receive a string from it.

<>Update: 完全正确。 缩短我的羊.时间。

问题回答
float getFloatFrom(EditText txt) {
    try {
        return NumberFormat.getInstance().parse(txt.getText().toString()).floatValue();
    } catch (ParseException e) {
        return 0.0f;
    }
}

如果你想要守则在使用礼让而不是mal点的国家工作,那么NOT会使用Float. ValueOf()。

努力案文 = editText.getText().toString();

浮动案文=Float.parseFloat(案文);





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...