簡體   English   中英

如何刪除插入到 textview 的 append 文本

[英]how can i remove an append text that I inserted to the textview

我在顯示 append 字並通過單擊 gridview 中選擇的項目來刪除字符串字值時遇到問題。

我想要這樣:

 ___________
|           |
|   hello   |        =      hello  (CLICK)
|___________|  

 ___________
|           |
|   hello   |        =          (UNCLICK)
|___________|  

例如:如果我單擊項目按鈕你好,它將向 textview 顯示“你好” ,當我單擊項目按鈕世界時,它將在文本視圖中顯示“你好世界” ,但如果我再次單擊項目按鈕你好,它將刪除你好在像這個“世界”這樣的文本視圖中

這是我的 onitemselect 代碼:

ll.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            public void onItemClick(AdapterView parent, View v, int position, long id) {
                                if(position < noOfBtns) {

                                    String selectedItem = parent.getItemAtPosition(position).toString();
                                    String s2 = text_quiz.getText().toString();
                                    String [] s2_word = s2.split("\\s");
                                    List<String> str = Arrays.asList(s2_word);
                                    if(str.contains(selectedItem))
                                    {
                                        if(str.equals(selectedItem)) {
                                            v.setBackgroundResource(R.drawable.btn_background);
                                            String rep = text_quiz.getText().toString().replace("\\b" + selectedItem + "\\b" + " ", "");
                                            String textToBeColored = "_";
                                            String htmlText = rep.replace(textToBeColored, "<font color='#6B3074'>" + textToBeColored + "</font>");
                                            text_quiz.setText(Html.fromHtml(htmlText));
                                        }
                                    }else {

                                                v.setBackgroundResource(R.drawable.buttons4);
                                                text_quiz.append(selectedItem + " ");

                                    }
                                }
                            }
                        });

在我的代碼中,我嘗試在 Textview 中將相同的選定項目替換為空白,但是當按鈕具有相同的單詞時,它將不再顯示。 就像的替換方法一樣,你的替換方法會找到相同的單詞並替換它,如果我有一個按鈕項單詞a它不會顯示代碼它將替換 textview 中的所有字母 a ..

我真的需要幫助

在高層次上,從那里你可以:

if (str.remove(selectedItem)) {
    // We know selectedItem was removed (the first, not sure if you have multiple)
    // so now we can just rebuild (here is a non stream version)
    StringBuilder sb = new StringBuilder();
    for (String st : str) {
        sb.append(textToBeColoured(str));  // Do whatever you want for whichever text
    }
    textQuiz.setText(Html.fromText(sb.toString());
} else {
   // Add like normal
}

您需要根據需要管理 HTML 突出顯示功能,可能會更改 for 以包含索引或其他內容。 但至少在這個if()塊中,您知道List<String> str已經刪除了您的文本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM