Android:Toast中的字符串连接不起作用

时间:2014-06-11 08:32:28

标签: java android toast string-concatenation

我设计了一个应用程序,其中一部分要求我根据剩余的输入字段数显示一个吐司。 这是我的代码片段 -

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:

我的错误在哪里?

已解决 - 而不是使用concat(),我使用text+="name"等等。 但为什么concat()没有工作?

5 个答案:

答案 0 :(得分:3)

if ((isEmpty(firstName))||(isEmpty(lastName))||(isEmpty(Age))||(isEmpty(phoneNumber)))
{
    String text="Following fields are empty:";
            if ((isEmpty(firstName))||(isEmpty(lastName))){
                text = text.concat("\nName");
            }
            if ((isEmpty(Age))){
                text = text.concat("\nAge");
            }
            if ((isEmpty(phoneNumber))){
                text = text.concat("\nPhone Number");
            }
            Toast toast=Toast.makeText(this, text, Toast.LENGTH_SHORT);
            toast.show();
        }

答案 1 :(得分:1)

更改text.concat("\nName");

text = text.concat("\nName");

同样适用于agePhone Number

即。只需使用

if ((isEmpty(firstName))||(isEmpty(lastName))||(isEmpty(Age))||(isEmpty(phoneNumber)))
{
    String text="Following fields are empty:";
            if ((isEmpty(firstName))||(isEmpty(lastName))){
                text = text.concat("\nName");
            }
            if ((isEmpty(Age))){
                text = text.concat("\nAge");
            }
            if ((isEmpty(phoneNumber))){
                text = text.concat("\nPhone Number");
            }
            Toast toast=Toast.makeText(this, text, Toast.LENGTH_SHORT);
            toast.show();
        }

或简单地用作

if ((isEmpty(firstName))||(isEmpty(lastName))||(isEmpty(Age))||(isEmpty(phoneNumber)))
    {
        String text="Following fields are empty:";
                if ((isEmpty(firstName))||(isEmpty(lastName))){
                    text +="\nName";
                }
                if ((isEmpty(Age))){
                    text +="\nAge";
                }
                if ((isEmpty(phoneNumber))){
                    text +="\nPhone Number";
                }
                Toast toast=Toast.makeText(this, text, Toast.LENGTH_SHORT);
                toast.show();
            }

答案 2 :(得分:0)

text.concat("\nName");更改为text = text.concat("\nName");

像:

if ((isEmpty(firstName))||(isEmpty(lastName))||(isEmpty(Age))||(isEmpty(phoneNumber)))
{
    String text="Following fields are empty:";
            if ((isEmpty(firstName))||(isEmpty(lastName))){
                text = text.concat("\nName");
            }
            if ((isEmpty(Age))){
                text = text.concat("\nAge");
            }
            if ((isEmpty(phoneNumber))){
                text = text.concat("\nPhone Number");
            }
            Toast toast=Toast.makeText(this, text, Toast.LENGTH_SHORT);
            toast.show();
        }

答案 3 :(得分:0)

每次调用concat()之后,都会创建一个新的String,因为Java中的字符串是不可变的'。在中,分配了一个新的对象实例,因此最好使用StringBuffer来连接多个字符串。

因此,正确的做法如下:

if ((isEmpty(firstName))||(isEmpty(lastName))||(isEmpty(Age))||(isEmpty(phoneNumber)))
{
    StringBuffer sb = new StringBuffer();
    sb.append("Following fields are empty:");
            if ((isEmpty(firstName))||(isEmpty(lastName))){
                sb.append("\nName");
            }
            if ((isEmpty(Age))){
                sb.append("\nAge");
            }
            if ((isEmpty(phoneNumber))){
                sb.append("\nPhone Number");
            }
            Toast toast=Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT);
            toast.show();
}

另外,之所以

string += "hello";

有效,是因为转换为

string = string + "hello";

答案 4 :(得分:0)

您也可以使用此:text += "\nName";