Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

android - How to retrieve multi-line text from Edittext?

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

I want below output when getText() from Edittext.

Output:

Lorem Ipsum is simply dummy

text of the printing and

typesetting industry. Lorem

Ipsum has been the industry

standard dummy text.

enter image description here

I have tried below solution but, it doesn't work

etMessage.getText().toString().replaceAll("\n", "<br />")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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("
"));
            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 + "
");
                                            }

                                        } 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("
" + lastWord.trim() + "
");
                                                } else {
                                                    Log.i(TAG, "fitString: ###### 2");
                                                    finalMessage.append("
" + lastWord.trim());
                                                }

                                            } else {

                                                if (j == (wordList.size() - 1) && i != (lineList.size() - 1)) {
                                                    Log.i(TAG, "fitString: ###### 3");
                                                    finalMessage.append(lastWord + "
");
                                                } 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) + "
");
                            }
                        }
                    } else {
                        Log.e(TAG, "fitString: Line is Null or Empty.");
                        finalMessage.append(lineList.get(i) + "
");
                    }
                }
            } 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 + "
");
                }
                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;
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...