我无法在Android应用中的TextView中设置一个字符串(使用Java)

huangapple go评论97阅读模式
英文:

I can't set a string in a TextView in an Android app (using Java)

问题

我正在用Java构建一个简单的Android计算器应用程序,它将接收2个数字作为输入,当用户按下4个操作按钮之一(+,-,*,/)时,练习及其解答将以以下格式显示在屏幕底部的TextView中:

{num1} {action} {num2} = {solution}

我尝试声明一个字符串并在其中组成练习的字符串,最后我使用“setText”来更改TextView,但是当我运行应用程序时,它显示的不是完整的练习,而是类似于“androidx.appcompat.widget.AppCom”。

这是用户单击“+”按钮时我形成的字符串示例:

exerciseStr = etNum1.toString() + " + " + etNum2.toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString()) + Integer.valueOf(etNum2.getText()) + "");

有谁知道可能的问题是什么吗?

英文:

I'm building a simple calculator Android app in Java that will receive 2 numbers as inputs and when the user presses one of the 4 action buttons (+, -, *, /) the exercise and it's solution will appear in the bottom of the screen inside a TextView in this format:

{num1} {action} {num2} = {solution}

I tried to declare a string and form the exercise's string in it and in the end I used "setText" to change the TextView but instead of showing the full exercise when I run the app it shows something like "androidx.appcompat.widget.AppCom".

Here is an example for the string I form when the user clicks on the + button:

exerciseStr = etNum1.toString() + " + " + etNum2.toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));

Does anybody know what the issue may be?

答案1

得分: 1

你应该在调用 toString() 之前调用 getText()

exerciseStr = etNum1.getText().toString() + " + " + etNum2.getText().toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString()) + Integer.valueOf(etNum2.getText()) + "");
英文:

You should call getText() befor calling toString():

exerciseStr = etNum1.getText().toString() + " + " + etNum2.getText().toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));

答案2

得分: 0

请将其更改为以下内容。

exerciseStr = etNum1.getText().toString() + " + " + etNum2.getText().toString() + " = "  + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));
英文:

Change it to like this.

exerciseStr = etNum1.getText().toString() + " + " + etNum2.getText().toString() + " = "  + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));

huangapple
  • 本文由 发表于 2020年9月25日 21:22:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64065021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定