首页 > 解决方案 > 我有一个文本视图,但我需要根据行的数据。前 4 行、3 行等

问题描述

我有一条来自 QR 码的文本。文本模式是这样的,整个文本位于 textview 中:

abc Ahtasham 在那里 def 不在那里 110 是 不是 120 等等等等

TEXTFEILD “abc”(必填) 免费 TEXTFIELD 用于地址

TEXTFIELD “def”(必填) 联系人免费 TEXTFIELD

我需要将前四行放在 textview 中,其他 2 行放在其他 textview 中,另外 2 行放在另一个 textview 中。我想知道如何在线获取数据。任何人都可以帮助。谢谢你。

标签: android

解决方案


像这样做

private String getLines(String txt, int aFrom, int aTo) {
    // create an array with all lines 
    String[] lines = txt.split("\\r?\\n");  
    String result = "";
    for (int i = aFrom; i <= aTo; i++) 
        result += lines[i] + "\n";
    return result;
}

像这样使用

textV2.setText(getLines(textV1.getText(), 0, 3)); // get first 4 lines
textV3.setText(getLines(textV1.getText(), 4, 5)); // get next 2 lines  
// and so on..

推荐阅读