0

String fullText = "Unser Ehrenpremierminister";Android Such Highlight Volles Wort

Zeichenfolge searchText = "onor";

Ich kann markieren "onor" Text gut. [Nach unten Codes]

Aber ich will "Ehren" Volltext markieren, wenn meine Suche Zeichenfolge "onor".

Mein Code ist:

public static void setSearchText(TextView textView, String fullText, String searchText) { 
    // highlight search text 
    if (null != searchText && !searchText.isEmpty()) { 
     int startPos = fullText.indexOf(searchText); 
     int endPos = startPos + searchText.length(); 
     if (startPos != -1) { 

      int ofe = fullText.indexOf(searchText, 0); 
      Spannable WordtoSpan = new SpannableString(fullText); 

      for (int ofs = 0; ofs < fullText.length() && ofe != -1; ofs = ofe + 1) { 
       ofe = fullText.indexOf(searchText, ofs); 
       if (ofe == -1) 
        break; 
       else { 
        ColorStateList redColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.RED}); 
        TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, redColor, null); 
        WordtoSpan.setSpan(highlightSpan, ofe, ofe + searchText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe + searchText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        WordtoSpan.setSpan(new RelativeSizeSpan(1.5f), ofe, ofe + searchText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        textView.setText(WordtoSpan, TextView.BufferType.SPANNABLE); 
       } 
      } 
     } else { 
      textView.setText(fullText); 
     } 

    } else { 
     textView.setText(fullText); 
    } 
} 
+0

Nach Teilzeichenfolge Lokalisieren Sie Suchen Sie nach "outward" bis "word" Grenzen, und markieren Sie diesen Bereich von Zeichen. –

+0

Verwenden Sie 'java.text.BreakIterator' – pskink

+0

Sie haben viele Beispiele in' BreakIterator' Dokumentation – pskink

Antwort

1

Sobald Sie ein Spiel für Ihren Suchbegriff in dem Volltext gefunden haben, müssen Sie die Wortgrenzen (Leerzeichen) das Match nach außen erweitern.

Dies ist eine Möglichkeit, es zu tun:

// für einen Artikel suchen

private static void setSearchText(TextView textView, final String fullText, final String searchText) { 
    // highlight search text 
    if (null != searchText && !searchText.isEmpty()) { 
    int startPos = fullText.indexOf(searchText); 
    int endPos = startPos + searchText.length(); 

    if (startPos != -1) { 
     // Found a match to the partial text -- now search outward to 
     // the word boundaries 
     final char WORD_BOUNDARY = ' '; 
     final char WORD_BOUNDARY1 = '\n'; 

     int wordStart = startPos; 
        while (wordStart >= 0 && fullText.charAt(wordStart) != WORD_BOUNDARY && fullText.charAt(wordStart) != WORD_BOUNDARY1) { 
         --wordStart; 
        } 
        wordStart = wordStart + 1; 

        int wordEnd = endPos; 
        while (wordEnd < fullText.length() && fullText.charAt(wordEnd) != WORD_BOUNDARY && fullText.charAt(wordEnd) != WORD_BOUNDARY1) { 
         ++wordEnd; 
        } 

     // Now highlight based on the word boundaries 
     ColorStateList redColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.RED}); 
     TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, redColor, null); 

     Spannable wordtoSpan = new SpannableString(fullText); 

     wordtoSpan.setSpan(highlightSpan, wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
     wordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
     wordtoSpan.setSpan(new RelativeSizeSpan(1.5f), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

     textView.setText(wordtoSpan, TextView.BufferType.SPANNABLE); 
    } else { 
     textView.setText(fullText); 
    } 
    } else { 
    textView.setText(fullText); 
    } 
} 

// Für Metasuchen Verwenden String folgenden Codes:

public static void setSearchText(TextView textView, final String fullText, final String searchText) { 
    // highlight search text 
    if (null != searchText && !searchText.isEmpty()) { 
     int startPos = fullText.indexOf(searchText, 0); 
     int endPos = startPos + searchText.length(); 

     if (startPos != -1) { 
      // Found a match to the partial text -- now search outward to 
      // the word boundaries 

      Spannable wordtoSpan = new SpannableString(fullText); 

      for (int i = 0; i < fullText.length() && startPos != -1; i = startPos + 1) { 
       startPos = fullText.indexOf(searchText, i); 
       endPos = startPos + searchText.length(); 
       if (startPos == -1) 
        break; 
       else { 
        final char WORD_BOUNDARY = ' '; 
        final char WORD_BOUNDARY1 = '\n'; 

        int wordStart = startPos; 
        while (wordStart >= 0 && fullText.charAt(wordStart) != WORD_BOUNDARY && fullText.charAt(wordStart) != WORD_BOUNDARY1) { 
         --wordStart; 
        } 
        wordStart = wordStart + 1; 

        int wordEnd = endPos; 
        while (wordEnd < fullText.length() && fullText.charAt(wordEnd) != WORD_BOUNDARY && fullText.charAt(wordEnd) != WORD_BOUNDARY1) { 
         ++wordEnd; 
        } 

        // Now highlight based on the word boundaries 
        ColorStateList redColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.RED}); 
        TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, redColor, null); 

        wordtoSpan.setSpan(highlightSpan, wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        wordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        wordtoSpan.setSpan(new RelativeSizeSpan(1.5f), wordStart, wordEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

        textView.setText(wordtoSpan, TextView.BufferType.SPANNABLE); 

       } 
      } 
     } else { 
      textView.setText(fullText); 
     } 
    } else { 
     textView.setText(fullText); 
    } 
}