如何从 EditText 返回一个 int?(安卓)

2022-01-14 00:00:00 android java android-edittext

基本上,我想要一个 Android 中的 EditText,我可以在其中输入一个整数值.也许有比 EditText 更合适的对象?

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?

推荐答案

现在,使用 EditText.使用 android:inputType="number" 将其强制为数字.将结果字符串转换为整数(例如,Integer.parseInt(myEditText.getText().toString())).

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())).

将来,您可能会考虑使用 NumberPicker 小部件,一旦该小部件可用(预定在 Honeycomb 中).

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

相关文章