我有字符串数组项,但每完成一个元素,我需要移动到下一行。
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.
但输出应该在每一行。
试试这个
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");
}
}
}
用这个 :
broadcastMessage.setText("\n" + Arrays.toString(word).replaceAll("\\[|\\]", ""));
android:singleLine = "false"
在xml和broadcastMessage.setText("\n" + Arrays.toString(word).replaceAll("\\[|\\]", ""));
试试这样:
String abc="" + Arrays.toString(word).replaceAll("\\[|\\]", "");
abc.replaceAll(",","\n");
broadcastMessage.setText(abc);
如果您使用的是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));
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);
}
}
}