【发布时间】:2014-08-01 05:32:57
【问题描述】:
我设计了一个应用程序,其中一部分要求我根据有多少输入字段为空来显示祝酒词。 这是我的代码 sn-p-
if ((isEmpty(firstName))||(isEmpty(lastName))||(isEmpty(Age))||(isEmpty(phoneNumber)))
{
String text="Following fields are empty:";
if ((isEmpty(firstName))||(isEmpty(lastName))){
text.concat("\nName");
}
if ((isEmpty(Age))){
text.concat("\nAge");
}
if ((isEmpty(phoneNumber))){
text.concat("\nPhone Number");
}
Toast toast=Toast.makeText(this, text, Toast.LENGTH_SHORT);
toast.show();
}
这里isEmpty()如果输入字段为空则返回True,否则返回false。
现在,我希望输出(当所有字段为空时)显示
Following fields are empty:
Name
Age
Phone Number
但是,它只显示Following fields are empty:。
我的错在哪里?
已解决- 我使用了text+="name" 等等,而不是 concat()。
但是为什么 concat() 不起作用?
【问题讨论】:
-
我很确定是换行符。你确定 Toast 天生就支持多行吗?
-
不,这是使用 concat() 的问题。查看我更新的问题
-
Android 基本上是使用 Java。我有点脱节,但希望您需要使用 text = text.concat("\nName");
-
Ohhhhh... 这是因为字符串的不变性。在每次调用“concat”时,都会创建一个新字符串。抱歉,我之前没有注意到这个错误。
-
查看我的答案以获取更多信息。
标签: java android toast string-concatenation