久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何從 Edittext 中檢索多行文本?

How to retrieve multi-line text from Edittext?(如何從 Edittext 中檢索多行文本?)
本文介紹了如何從 Edittext 中檢索多行文本?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我想從 Edittext 中獲取與給定屏幕截圖相同的文本(多行).

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

當從 Edittext 獲取文本()時,我想要下面的輸出.

I want below output when getText() from Edittext.

輸出:

Lorem Ipsum 只是假的

Lorem Ipsum is simply dummy

印刷文字和

排版行業.洛雷姆

Ipsum一直是行業

標準虛擬文本.

我嘗試了以下解決方案,但它不起作用

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

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

推薦答案

經過太多的搜索和等待這個問題的答案.我已經解決了這個問題.

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;
        }
    }

這篇關于如何從 Edittext 中檢索多行文本?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cut, copy, paste in android(在android中剪切、復制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數的數字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 高清国产午夜精品久久久久久 | 国产在线观看一区二区 | 无码一区二区三区视频 | 日日日干干干 | 日韩欧美国产一区二区 | 91传媒在线观看 | 99久久婷婷国产精品综合 | 亚洲日本一区二区三区四区 | 色资源在线 | 伊人狼人影院 | 9久9久 | 亚洲三级在线观看 | 亚洲欧美在线一区 | 国产欧美精品一区二区色综合朱莉 | 91精品国产91久久久久久最新 | 黄色大片免费网站 | 欧美91 | 亚洲国产精久久久久久久 | 日韩免费一级 | 欧美日韩免费一区二区三区 | 在线电影日韩 | 亚洲精选久久 | 狠狠干av | 正在播放一区二区 | 日韩视频在线一区 | 欧美99| 麻豆精品国产91久久久久久 | 久久精品久久综合 | 欧美精品一区二区三区在线播放 | 日韩综合网 | 欧美精品久久久久久久久久 | 国产在线a | 狠狠操狠狠色 | 羞羞视频网 | 先锋av资源在线 | 91免费在线看 | 国产亚洲精品精品国产亚洲综合 | 精品9999| 青青草视频网 | 国产一区不卡 | 免费看国产a |