Android - 获取用户输入的整数
我是一名新的 Android 开发人员。作为一个起始项目,我正在尝试创建一个基本的加法计算器。我有一个 EditText
,它应该接受输入(输入是一个字符串),并在按下 Button1
时将其转换为 int1
。当按下 Button2
时,它应该接受输入,将其转换为 int2
,添加 int1
和 int2
并将结果存储在 int ans
中,并将 EditText
的文本设置为 ans。但是,当我尝试使用 Integer.parseInt(et.getText().toString()) 时,出现错误并且应用程序强制关闭。谁能给我提供将这些字符串正确转换为整数的代码?谢谢。
static int fn = 0;
static int sn = 0;
static int ans = 0;
static int pro = 0;
//"+" Button Clicked//
if(pro == 0){
fn = Integer.parseInt(entry.getText().toString());
entry.setText("");
pro++;
}else{
//MessageBox Crap//
//"=" Button Clicked//
sn = Integer.parseInt(entry.getText().toString());
ans = fn + sn;
entry.setText(ans);
I'm a new Android developer. As a starting project, I'm trying to create a basic addition calculator. I have an EditText
which is supposed to take the input (input is a string) and convert it to int1
when Button1
is pressed. When Button2
is pressed, it is supposed to take the input, convert it to int2
, add int1
and int2
together and store the result in the int ans
, and set the text of the EditText
to ans. However, when I try to use Integer.parseInt(et.getText().toString())
I get an error and the app force closes. Could anyone provide me with the code to properly convert these Strings to integers? Thank you.
static int fn = 0;
static int sn = 0;
static int ans = 0;
static int pro = 0;
//"+" Button Clicked//
if(pro == 0){
fn = Integer.parseInt(entry.getText().toString());
entry.setText("");
pro++;
}else{
//MessageBox Crap//
//"=" Button Clicked//
sn = Integer.parseInt(entry.getText().toString());
ans = fn + sn;
entry.setText(ans);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在设置 EditText 的内容之前,不应该将 ans 转换为字符串吗?
Shouldn't ans be converted to a string before you set the contents of the EditText?