如何从Edittext

时间:2019-05-23 06:48:40

标签: android android-layout android-edittext

我想从Edittext获取与给定的屏幕截图相同的文本(多行)。

我想从Edittext获得getText()时在下面的输出。

输出:

Lorem Ipsum只是假的

印刷文字和

排版行业。洛雷姆

Ipsum一直是行业

标准伪文本。

enter image description here

我已经尝试过以下解决方案,但它不起作用

etMessage.getText().toString().replaceAll("\\n", "<br />")

4 个答案:

答案 0 :(得分:2)

经过过多搜索并等待对此问题的答案。我已经解决了这个问题。

解决方案: 我已经测量了每一行和每个单词,以将其保存为多行文本,您可以使用以下功能。

DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
float density = metrics.density;

String result = fitString(ipText, ipText.getText().toString());

private String fitString(EditText editText, String message) {

        Log.i(TAG, "fitString: Default String : " + message);

        StringBuilder finalMessage = new StringBuilder();

        if (isTooLarge(editText, message)) {
            Log.i(TAG, "fitString: isTooLarge 1 : " + true);
            List<String> lineList = Arrays.asList(message.split("\n"));
            Log.i(TAG, "fitString: stringList" + lineList);

            if (lineList != null && lineList.size() > 0) {

                for (int i = 0; i < lineList.size(); i++) {

                    if (lineList.get(i) != null && !lineList.get(i).isEmpty()) {

                        if (isTooLarge(editText, lineList.get(i))) {
                            Log.i(TAG, "fitString: isTooLarge 2 : " + lineList.get(i) + " == " + true);

                            List<String> wordList = Arrays.asList(lineList.get(i).split(" "));
                            Log.i(TAG, "fitString: wordList" + wordList);

                            if (wordList != null && wordList.size() > 0) {
                                Log.i(TAG, "fitString: wordList : " + wordList.size());

                                StringBuilder temp = new StringBuilder();
                                String lastWord = "";

                                for (int j = 0; j < wordList.size(); j++) {

                                    if (wordList.get(j) != null && !wordList.get(j).isEmpty()) {

                                        if (isTooLarge(editText, wordList.get(j))) {
                                            Log.i(TAG, "fitString: isTooLarge 3 : " + wordList.get(j) + " == " + true);
                                            String newString = fitCharacter(editText, wordList.get(j));
                                            Log.i(TAG, "fitString: fitCharacter == " + newString);

                                            if (j == (wordList.size() - 1) && i == (lineList.size() - 1)) {
                                                finalMessage.append(newString);
                                            } else {
                                                finalMessage.append(newString + "\n");
                                            }

                                        } else {

                                            if (j == 0) {
                                                lastWord = wordList.get(j);
                                            } else {
                                                lastWord = " " + wordList.get(j);
                                            }


                                            temp.append(lastWord);
                                            Log.i(TAG, "fitString: temp : " + temp);
                                            Log.i(TAG, "fitString: lastWord : " + lastWord);

                                            if (isTooLarge(editText, temp.toString())) {
                                                temp.setLength(0); // clear String Builder,  new StringBuilder()
                                                temp.append(lastWord);
                                                if (j == (wordList.size() - 1) && i != (lineList.size() - 1)) {
                                                    Log.i(TAG, "fitString: ###### 1");
                                                    finalMessage.append("\n" + lastWord.trim() + "\n");
                                                } else {
                                                    Log.i(TAG, "fitString: ###### 2");
                                                    finalMessage.append("\n" + lastWord.trim());
                                                }

                                            } else {

                                                if (j == (wordList.size() - 1) && i != (lineList.size() - 1)) {
                                                    Log.i(TAG, "fitString: ###### 3");
                                                    finalMessage.append(lastWord + "\n");
                                                } else {
                                                    Log.i(TAG, "fitString: ###### 4");
                                                    finalMessage.append(lastWord);
                                                }

                                            }

                                            Log.i(TAG, "fitString: finalMessage : " + finalMessage);
                                        }

                                    } else {
                                        Log.e(TAG, "fitString: Word is Null or Empty.");
                                        finalMessage.append(" ");
                                    }

                                }

                            } else {
                                Log.e(TAG, "fitString: wordList is Null or Empty.");
                            }


                        } else {
                            Log.i(TAG, "fitString: isTooLarge 2 : " + lineList.get(i) + " == " + false);
                            if (i == (lineList.size() - 1)) {
                                finalMessage.append(lineList.get(i));
                            } else {
                                finalMessage.append(lineList.get(i) + "\n");
                            }
                        }
                    } else {
                        Log.e(TAG, "fitString: Line is Null or Empty.");
                        finalMessage.append(lineList.get(i) + "\n");
                    }
                }
            } else {
                Log.e(TAG, "fitString: stringList is Null or Empty.");
                finalMessage.append("");
            }

            return finalMessage.toString();

        } else {
            Log.i(TAG, "fitString: isTooLarge : " + false);
            return message;
        }
    }

    public String fitCharacter(EditText editText, String message) {

        Log.i(TAG, "fitCharacter2: Default Word : " + message);

        StringBuilder finalWord = new StringBuilder();
        int startIndex = 0;
        int endIndex = 1;


        for (; ; ) {

            String tempSplitWord = message.substring(startIndex, endIndex);
            Log.i(TAG, "fitCharacter2: startIndex : " + startIndex + " endIndex : " + endIndex + " tempSplitWord : " + tempSplitWord);
            if (!isTooLarge(editText, tempSplitWord)) { // isTooLarge
                if (endIndex < message.length()) {
                    endIndex = endIndex + 1;
                    Log.i(TAG, "IF fitCharacter2: endIndex < message.length() " + endIndex + " < " + message.length());
                } else {
                    String result = finalWord.append(tempSplitWord).toString();
                    Log.i(TAG, "IF RETURN RESULT : " + result);
                    return result;
                }
            } else {
                endIndex = endIndex - 1;
                String splitWord = message.substring(startIndex, endIndex);
                Log.i(TAG, "ELSE fitCharacter2: startIndex : " + startIndex + " endIndex : " + endIndex + " splitWord : " + splitWord);

                boolean isTooLarge = isTooLarge(editText, splitWord);
                if (!isTooLarge) {
                    finalWord.append(splitWord + "\n");
                }
                startIndex = endIndex;
                endIndex = endIndex + 1;
                Log.i(TAG, "ELSE fitCharacter2: startIndex : " + startIndex + " endIndex : " + endIndex);
            }
        }
    }

    private boolean isTooLarge(EditText editText, String newText) {
        if (editText != null && editText.getPaint() != null) {
            float textWidth = editText.getPaint().measureText(newText);

            return (textWidth >= (editText.getMeasuredWidth() - (12 * density))); // editText.getMeasuredWidth();
        } else {
            return false;
        }
    }

答案 1 :(得分:0)

您尝试过这个

message = etMessage.getText().toString().replaceAll("\\n", "<br />")

也请How can I preserve line breaks from EditText?

查看

答案 2 :(得分:0)

默认情况下,Android中的所有EditText小部件都是多行的。您可以配置行数和字符类型。通过将输入类型设置为多行即可达到目的。

[Authorize(Roles = "Administrator")]
public IActionResult Get()
{
    // ...
}

答案 3 :(得分:0)

对于下一个来者,我发现该任务过于复杂。这是Kotlin中使用Paint.breakText()的不错的扩展代码。也就是说,它可能可以进一步简化...

fun EditText.getMultilineText(): String {
    val maxWidth = (width - paddingLeft - paddingRight).toFloat()
    val original = text.toString().trim()
    val len = original.length
    val multiline = mutableListOf<String>()
    var p = 0

    var count = -1
    while (count != 0) {
        count = paint.breakText(original, p, len, true, maxWidth, null)
        if (p + count < len) {
            var tmp = count
            while (tmp > 0 && original[p + tmp - 1] != ' ') {
                tmp -= 1
            }
            if (tmp > 0) {
                count = tmp
            }
        }
        val tmp = original.substring(p, p + count).trim()
        if (tmp.isNotBlank()) {
            multiline.add(tmp)
        }
        p += count
    }

    return multiline.joinToString("\r\n")
}