Converting EditText to int? (Android)

23

I am wondering how to convert an edittext input to an int, I have the user input a number, it than divides it by 8.

MainActivity.java

@SuppressWarnings("unused")
public void calcSpeed(View view)
{       
    setContentView(R.layout.activity_speed);     

    final TextView mTextView = (TextView) findViewById(R.id.textView3);
    mTextView.setText("You should be getting: " +netSpedCalcd);
}

activity_main.xml

    <EditText
    android:id="@+id/editText1" 
    android:inputType="number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="62dp"
    android:ems="10" >

This question is tagged with android integer android-edittext int

~ Asked on 2013-02-23 05:34:45

The Best Answer is


42

you have to used.

String value= et.getText().toString();
int finalValue=Integer.parseInt(value);

if you have only allow enter number then set EditText property.

android:inputType="number"

if this is helpful then accept otherwise put your comment.

~ Answered on 2013-02-23 05:43:28


9

Use Integer.parseInt, and make sure you catch the NumberFormatException that it throws if the input is not an integer.

~ Answered on 2013-02-23 05:39:01


Most Viewed Questions: