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

问题描述 投票:0回答:6

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

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.

但输出应该在每一行。

java android string replace
6个回答
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");
        }



    }


}

0
投票

用这个 :

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

0
投票

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


0
投票

试试这样:

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

0
投票

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

String[] word = { "This is text1.So it should be single line", "This is text2", "This is text3" };
String separator = System.getProperty("line.separator");
Arrays.asList(word).forEach(w -> broadcastMessage.append(w+separator));

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);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.