[英]I can't set a string in a TextView in an Android app (using Java)
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:我正在用 Java 构建一个简单的计算器 Android 应用程序,它将接收 2 个数字作为输入,当用户按下 4 个操作按钮(+、-、*、/)之一时,练习及其解决方案将出现在底部此格式的 TextView 中的屏幕:
{num1} {action} {num2} = {solution} {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".我尝试声明一个字符串并在其中形成练习的字符串,最后我使用“setText”来更改 TextView,但在运行应用程序时没有显示完整的练习,而是显示类似“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() + ""));练习Str = etNum1.toString() + " + " + etNum2.toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));
Does anybody know what the issue may be?有人知道可能是什么问题吗?
您应该在调用toString()
之前调用getText()
toString()
:
exerciseStr = etNum1.getText().toString() + " + " + etNum2.getText().toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));
改成这样。
exerciseStr = etNum1.getText().toString() + " + " + etNum2.getText().toString() + " = " + String.valueOf(Integer.valueOf(etNum1.getText().toString())+Integer.valueOf(etNum2.getText() + ""));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.