如何在textview中为字符串数组引入下一行?

时间:2018-03-01 07:24:13

标签: java android string replace

我有字符串数组项,但是对于每个元素的完成,我需要移到下一行。

String[] word = { "This is text1.So it should be single line", "This is text2", "This is text3" };

broadcastMessage.setText("" + Arrays.toString(word).replaceAll("\\[|\\]", ""));

但是这个输出显示为,

This is text1.So it should be single line ,this is text2,this is text3.

预期输出:(应该没有逗号,应该是下一行)。

This is text1.So it should be single line
This is text2
This is text3.

但输出应该在每一行。

6 个答案:

答案 0 :(得分:1)

试试这个

public class MainActivity extends AppCompatActivity {    

    String[] word = {"This is text1.So it should be single line", "This is text2", "This is text3"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 


        TextView textView = findViewById(R.id.textureView1);
        for (int i = 0; i < word.length; i++) {
            textView.append(word[i]);
            textView.append("\n");
        }



    }


}

答案 1 :(得分:0)

使用此:

broadcastMessage.setText("\n" + Arrays.toString(word).replaceAll("\\[|\\]", ""));

答案 2 :(得分:0)

xml和中的

android:singleLine = "false" broadcastMessage.setText("\n" + Arrays.toString(word).replaceAll("\\[|\\]", ""));

答案 3 :(得分:0)

试试这样:

String abc="" + Arrays.toString(word).replaceAll("\\[|\\]", "");
    abc.replaceAll(",","\n");
    broadcastMessage.setText(abc);

答案 4 :(得分:0)

public class MainActivity extends AppCompatActivity {
    String[] words = {"This is text1.So it should be single line", "This is text2", "This is text3"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        TextView textView = findViewById(R.id.textureView);
        for (String string : words) {
            textView.setText(textView.getText().toString() + "\n" + string);
        }
    }
}

答案 5 :(得分:0)

如果您使用的是Java 8+,则可以将以下代码与lambda表达式一起使用:

$ python 1.py
[u'34.244.138.72', u'34.249.95.222']