【问题标题】:Android:String concatenation in Toast doesn't workAndroid:Toast 中的字符串连接不起作用
【发布时间】: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


【解决方案1】:
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();
        }

【讨论】:

    【解决方案2】:

    更改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();
                }
    

    【讨论】:

    • 是的,这就是造成麻烦的原因。谢谢。
    • @Deathstroke,很乐意提供帮助,如果有帮助,请接受。
    • 我要去。它说我需要等待 8 分钟。别担心,我会接受的。
    • @Deathstroke 谢谢,您也可以使用我编辑的答案。无论如何,享受编码。
    • @Deathstroke 在stackoverflow.com/questions/2818796/…@Deathstroke 了解为什么 concat 无法在您的情况下工作
    【解决方案3】:

    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();
            }
    

    【讨论】:

    • 是的,解决了它。谢谢
    【解决方案4】:

    每次调用 concat() 后,都会创建一个新字符串,因为 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";
    

    【讨论】:

      【解决方案5】:

      你也可以使用这个:text += "\nName";

      【讨论】:

      • 是的,这就是我用的
      猜你喜欢
      • 2021-01-26
      • 2018-09-03
      • 2013-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-03
      • 2021-01-28
      相关资源
      最近更新 更多