How do I return an int from EditText? (Android)


Question

Basically, I want an EditText in Android where I can have an integer value entered into. Perhaps there is a more appropriate object than EditText for this?

1
65
11/27/2017 10:43:49 PM

Accepted Answer

For now, use an EditText. Use android:inputType="number" to force it to be numeric. Convert the resulting string into an integer (e.g., Integer.parseInt(myEditText.getText().toString())).

In the future, you might consider a NumberPicker widget, once that becomes available (slated to be in Honeycomb).

136
2/4/2011 10:20:30 PM

Set the digits attribute to true, which will cause it to only allow number inputs.

Then do Integer.valueOf(editText.getText()) to get an int value out.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon