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

时间:2021-06-08 21:45:40

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中,我可以输入一个整数值。也许还有比EditText更合适的对象?

4 个解决方案

#1


128  

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

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

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

将来,您可能会考虑一个NumberPicker小部件,一旦它可用(预定在蜂巢中)。

#2


10  

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

将digit属性设置为true,这将导致它只允许数字输入。

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

然后执行Integer.valueOf(editText.getText())来获取一个int值。

#3


8  

First of all get a string from an EDITTEXT and then convert this string into integer like

首先从EDITTEXT获取一个字符串,然后将该字符串转换为整数

      String no=myTxt.getText().toString();       //this will get a string                               
      int no2=Integer.parseInt(no);              //this will get a no from the string

#4


0  

You can do this in 2 ways:

你可以用两种方法:

1: Change the input type(In your EditText field) in the layout file to android:inputType="number"

1:将布局文件中的输入类型(在EditText字段中)更改为android:inputType=“number”

Or

2: Use int a = Integer.parseInt(yourEditTextObject.getText().toString());

2:使用int a = Integer.parseInt(yourEditTextObject.getText().toString());

#1


128  

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

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

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

将来,您可能会考虑一个NumberPicker小部件,一旦它可用(预定在蜂巢中)。

#2


10  

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

将digit属性设置为true,这将导致它只允许数字输入。

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

然后执行Integer.valueOf(editText.getText())来获取一个int值。

#3


8  

First of all get a string from an EDITTEXT and then convert this string into integer like

首先从EDITTEXT获取一个字符串,然后将该字符串转换为整数

      String no=myTxt.getText().toString();       //this will get a string                               
      int no2=Integer.parseInt(no);              //this will get a no from the string

#4


0  

You can do this in 2 ways:

你可以用两种方法:

1: Change the input type(In your EditText field) in the layout file to android:inputType="number"

1:将布局文件中的输入类型(在EditText字段中)更改为android:inputType=“number”

Or

2: Use int a = Integer.parseInt(yourEditTextObject.getText().toString());

2:使用int a = Integer.parseInt(yourEditTextObject.getText().toString());