如何将EditText转换为Float

时间:2015-04-07 14:23:23

标签: java android android-edittext numbers

我想将EditText转换为float,但应用程序已停止,我不知道我做错了什么..

这是来自java文件的代码:

 EditText janeiro1 = (EditText) findViewById(R.id.editText1);
 float janeiro = Float.valueOf(janeiro1.getText().toString());

这是.xml文件中的代码:

 <EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:inputType="numberDecimal"
      android:ems="10"
      android:id="@+id/editText2"
      android:layout_above="@+id/textView3"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true" />

这是logcat错误输出:

java.lang.RuntimeException: Unable to start activity
    ComponentInfo{com.example.kiowa.myapp/com.example.kiowa.myapp.c_detalhado}:
java.lang.NumberFormatException: Invalid float: ""
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
    at android.app.ActivityThread.access$800(ActivityThread.java:144)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NumberFormatException: Invalid float: ""
    at java.lang.StringToReal.invalidReal(StringToReal.java:63)
    at java.lang.StringToReal.parseFloat(StringToReal.java:308)
    at java.lang.Float.parseFloat(Float.java:306)
    at com.example.kiowa.myapp.c_detalhado.onCreate(c_detalhado.java:39)
    at android.app.Activity.performCreate(Activity.java:5933)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
    at android.app.ActivityThread.access$800(ActivityThread.java:144)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)

2 个答案:

答案 0 :(得分:1)

    usernameInput = (EditText) findViewById(R.id.username); // inOncreateview

    // Somewhere in method
    float a;

    try{
if(!usernameInput.getText().toString().equalsignorecase(""))
    a = Float.parseFloat(usernameInput.getText().toString());
    }catch(NumberFormatException e){
    log.d("Error","Not Convertible");
    e.printstacktrace();
    }

答案 1 :(得分:0)

EditText usernameInput = (EditText) findViewById(R.id.username);
float a = 0f;
try {
    a = Float.parseFloat(usernameInput.getText().toString());
} catch (Exception e) {
    a = 0f;
}

已更新

您的代码中还有另一个错误。 我已对此进行了测试,并且工作正常。

<强> MainActivity.java

public class MainActivity extends ActionBarActivity implements OnClickListener {
    EditText editText;
    TextView textView;
    Button button1;
    float a;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText) findViewById(R.id.editText1);
        textView = (TextView) findViewById(R.id.textView1);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(this);

    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button1) {
            try {
                a = Float.parseFloat(editText.getText().toString());
                textView.setText(String.valueOf(a));
            } catch (Exception e) {
                a = 0f;
                textView.setText(String.valueOf(a));
            }
        }

    }   
}

<强> activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test.MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="33dp"
    android:ems="10" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="17dp"
    android:text="Button" />