How to retrieve multi-line text from Edittext?

后端 未结 4 1168
有刺的猬
有刺的猬 2020-12-17 06:08

I want to get a text(Multi-line) from Edittext same as given Screenshot.

I want below output when getText() from Edittext.

Output:

相关标签:
4条回答
  • 2020-12-17 06:56

    did you try this one

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

    please see this also How can I preserve line breaks from EditText?

    0 讨论(0)
  • 2020-12-17 06:58

    For next comers, I find the accepted answer overcomplicated for the task. Here is a nice extension code in Kotlin that uses Paint.breakText(). That said, it can probably be simplified further...

    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")
    }
    
    0 讨论(0)
  • 2020-12-17 07:06

    By default all the EditText widgets in Android are multi-lined. And you can configure the number of lines and the characters types. By setting the input type to multiline do the trick.

    <EditText 
       ...
       android:inputType="textMultiLine" <!-- Multiline input -->
       ...
       android:lines="8" <!-- Total Lines prior display -->
       android:minLines="6" <!-- Minimum lines -->
       android:gravity="top|left" <!-- Cursor Position -->
       android:maxLines="10" <!-- Maximum Lines -->
       android:layout_height="wrap_content" <!-- Height determined by content -->
       android:layout_width="match_parent" <!-- Fill entire width -->
       android:scrollbars="vertical" <!-- Vertical Scroll Bar -->
    />
    
    0 讨论(0)
  • 2020-12-17 07:08

    After too much searching and waiting for an answer to this question. I have resolved this issue.

    Solution: I have measured each and every line & words for preserve this as Multiline text, you can use below function for that.

    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;
            }
        }
    
    0 讨论(0)
提交回复
热议问题