Java 将文本附加到 TextView 数据类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29636230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:21:04  来源:igfitidea点击:

Append text to a TextView datatype

javaandroidandroid-studiotextview

提问by snapplex

I'm a beginner android/java programmer and my background is primarily in C++ and C#. In C# if I have a string variable called myWord and it has a value of "Hello" I can append additional information by using the + operator.

我是一名初学者 android/java 程序员,我的背景主要是 C++ 和 C#。在 C# 中,如果我有一个名为 myWord 的字符串变量并且它的值为“Hello”,我可以使用 + 运算符附加其他信息。

I tried this method a few times in java and apparently I can't use this tatic because the TextView datatype is void. Android studio gives me the following error: Operator '+' cannot be applied to 'void', 'java.lang.String'

我在java中尝试了几次这种方法,显然我不能使用这个tatic,因为TextView数据类型是无效的。Android studio 给了我以下错误:操作符 '+' 不能应用于 'void', 'java.lang.String'

/*C # */
public string bob ()
{
return "Bob!";
}

string myWord = "Hello ";
myWord = myWord + "Bob!"; //myWord is now equal to "Hello Bob!"

OR 

myWord = "Hello " + bob(); //myWord is now equal to "Hello Bob!"


*/ JAVA */
TextView displayTextView = null;
displayTextView.setText("Hello");

I'd like to find a way to append additional text to the original value of "Hello" so the value of displayTextView will be "Hello Bob!"

我想找到一种方法将附加文本附加到“Hello”的原始值,因此 displayTextView 的值将是“Hello Bob!”

Any ideas?

有任何想法吗?

Edit: I'm currently using a capital "S" and my app can successfully access and retrieve information from my class. The app would FC whenver I tried to append text to to the TextView data type.

编辑:我目前使用大写的“S”,我的应用程序可以成功访问和检索我的班级信息。每当我尝试将文本附加到 TextView 数据类型时,该应用程序都会 FC。

Thank you for the feedback everyone.

谢谢大家的反馈。

采纳答案by Longi

You can call append()on a TextView object.

您可以调用append()TextView 对象。

In your case it would be: displayTextView.append("Bob!");

在您的情况下,它将是: displayTextView.append("Bob!");

回答by Shihab

First letter of String is not small letter. To take a String variable in java you have to write String var;

String 的第一个字母不是小写字母。要在 java 中获取 String 变量,您必须编写 String var;

So, for android use following code:

因此,对于 android 使用以下代码:

TextView displayTextView = null;
TextView displayTextView = (TextView) findViewById(R.id.myText);
String myWord = "Your";
displayTextView.setText("Hello " + myword);

This should work.

这应该有效。

回答by mrek

If you are using TextView, use append. For example:

如果您使用的是 TextView,请使用append。例如:

TextView textView = (TextView) findViewById(R.id.myText);
textView.setText("Hello");
textView.append(" Bob!");

回答by deadfish

Why nobody suggested getText()method ?

为什么没有人建议getText()方法?

TextView displayTextView = null;
displayTextView.setText("text1");
displayTextView.setText(displayTextView.getText() + "text2");//poor and weak

or better for longer strings:

或者对于更长的字符串更好:

SpannableString ss =new SpannableString();
ss.append("text1").append("text2");
displayTextView.setText(ss);

回答by TejjD

Your issue is on your declaration of the String instance in both the method and the variable.

您的问题在于您在方法和变量中对 String 实例的声明。

It requires a "S" not a lower case s.

它需要一个“S”而不是小写的 s。

Also the "+" sign does work, it is just your String declaration as pointed out.

“+”号也确实有效,它只是您指出的 String 声明。

Here is how it looks

这是它的外观

All the best :)

祝一切顺利 :)