输入字段&将文本转换为数字

发布于 2024-09-30 19:55:00 字数 204 浏览 1 评论 0原文

我正在开发一个应用程序,它对一些变量进行一些数学计算并输出答案。无论如何,这就是计划。成为 Android 和 Android 领域的佼佼者。 Java 我不确定一些事情。

1)我在布局文件中创建了文本字段,供用户输入值。如果存在这种情况,我应该使用数值字段而不是文本字段吗?

2) 如何在文本字段和数值之间进行转换?

非常感谢您的帮助 蒂姆

I have an app I am working on that does some math on a few variables and outputs the answer. Well that's the plan anyway. Being a total n00b at Android & Java I am not sure of a couple of things.

1) I have created text fields in the layout file for user to enter values. Instead of text fields should I use numerical value fields, if such a thing exists?

2) How can you convert from a text field to a numerical value and back again?

Many thanks for any help
Tim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等待我真够勒 2024-10-07 19:55:00

setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL )

或者在 xml 文件中我们可以将 edittext 字段的属性设置为 android:numeric="integer|decimal"。

setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL )

or in the xml file we can set the property of the edittext field to android:numeric="integer|decimal".

岁月无声 2024-10-07 19:55:00

为了从字符串转换为数值,我们可以使用:Integer.parseInt( string_to_convert )
例如

int your_variable1 = Integer.parseInt(str_to_be_converted1);
int your_variable2 = Integer.parseInt(str_to_be converted2);

另外,如果您正在计算面积,您的代码可以如下所示:

int area = Integer.parseInt(length)*Integer.parseInt(breadth);

For converting from string to numerical value we can use: Integer.parseInt( string_to_convert )
e.g.

int your_variable1 = Integer.parseInt(str_to_be_converted1);
int your_variable2 = Integer.parseInt(str_to_be converted2);

Also, if you are calculating the area your piece of code can be like this:

int area = Integer.parseInt(length)*Integer.parseInt(breadth);
2024-10-07 19:55:00

您可以将编辑文本属性设置为仅接受数字。 android:numeric=“整数|小数”。

使用 textview 小部件显示结果,使用 edittext 小部件获取输入。

接下来获取edittext字段中的内容(edittextobject.getText()),然后将内容转换为整数进行操作。

将结果转换回字符串并调用
textviewobject.setText(结果字符串)

You can set the edit text property to take only numbers. android:numeric="integer|decimal".

use textview widget to show the result and edittext widget to take the input.

Next get the content(edittextobject.getText())in the edittext fields and then convert the content to integer to operate .

convert the result back to string and call
textviewobject.setText(resultstring)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文