简体   繁体   English

如何将文本逐行设置为多行编辑文本

[英]How to set a text line by line to multi line edit text

I have a list of names and i need to add those names to multi line edit text box by line by line. 我有一个名称列表,我需要将这些名称逐行添加到多行编辑文本框中。 But here it is adding only last added name to edit text box.How can i resolve this can any give a suggestion. 但是在这里它仅添加了最后添加的名称以编辑文本框。我如何解决这个问题可以给个建议。

Here my code is: 这是我的代码:

 for(int i =0; i<Products.length; i++){

                    etItem.setText(Products[i]);
                }
String str = "";
for(int i =0; i<Products.length; i++){
  str  += Products[i]+"\n";
}
etItem.setText(str);

This could be as easy as: 这可能很简单:

etItem.setText(TextUtils.join("\n", Products));

You'll probably want to guard this with a null check too. 您可能也想通过null检查来保护它。

Your own code example sets the text for every product item, overwriting the previous value. 您自己的代码示例为每个产品项设置文本,覆盖先前的值。 Hence, only the last product item will ever display. 因此,将仅显示最后一个产品。

Alternatively, an approach that more resembles your original code would be to leverage the TextView#append(...) method. 另外,一种更类似于原始代码的方法是利用TextView#append(...)方法。 That'll be slightly more code than the one-liner above, as you'll have to ensure there is no line break before the first item (or after the last). 这将比上面的单行代码多一些代码,因为您必须确保在第一个项目之前(或最后一个项目之后)没有换行符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM