After Android English text click, get the English word of the current click and set the shadow

TextView control itself has many properties to control. If you want to get content, you only need getText() method to achieve it. At the same time, you can set various listeners for TextView. However, if you want to click to get part of the content in TextView, it's difficult to achieve it only by the function of TextView. For example, TextView text is a paragraph of English. How can I click each word to get the word content?

After unremitting efforts, I finally found a solution on stack overflow, which is said to be the best way to achieve this function by simply using TextView so far. It is arranged as follows:

First, set Spannable for textview in MainActivity, set the click word response method getEachWord() and set TextView Click to respond.

textView.setText(text, BufferType.SPANNABLE);
//Click on each word to respond                

//Click the response method getEachWord () as follows:
//Copy code
    public void getEachWord(TextView textView){        
        Spannable spans = (Spannable)textView.getText();        
        Integer[] indices = getIndices(                
                textView.getText().toString().trim(), ' ');        
        int start = 0;        
        int end = 0;          
        // to cater last/only word loop will run equal to the length of indices.length        
        for (int i = 0; i <= indices.length; i++) {            
            ClickableSpan clickSpan = getClickableSpan();                       
            // to cater last/only word            
            end = (i < indices.length ? indices[i] : spans.length());                        
            spans.setSpan(clickSpan, start, end,                    
            start = end + 1;        
        //Change the highlight color of the selected text        
    private ClickableSpan getClickableSpan(){         
        return new ClickableSpan() {                
            public void onClick(View widget) {                    
                TextView tv = (TextView) widget;                    
                String s = tv                            
                Log.d("tapped on:", s);                                                        
            public void updateDrawState(TextPaint ds) {                          
    public static Integer[] getIndices(String s, char c) {        
        int pos = s.indexOf(c, 0);        
        List<Integer> indices = new ArrayList<Integer>();        
        while (pos != -1) {            
            pos = s.indexOf(c, pos + 1);        
        return (Integer[]) indices.toArray(new Integer[0]);    

Copy code
The specific implementation process is as follows: 1. Convert TextView content to Spannable object; 2. Use getindexes method to divide text content into words according to space; 3. Add ClickableSpan for each word; 4. Copy onClick method during ClickableSpan object instantiation to extract the text content of the clicked part.

There are several points to pay attention to in this implementation process:

1. ClickableSpan has default properties, clickable hyperlinks are underlined by default, and are blue. If you need to change the default properties, you can add setColor() and setUnderlineText() methods to the updateDrawState() method of replication.

2. The setColor() mentioned above sets the text color of the span hyperlink, not the color after clicking. The highlightcolor after clicking belongs to the TextView attribute. The above Android 4.0 is light green by default, and the lower version is yellow. You can use textView.setHighlightColor(Color.BLUE) to change the color.

3. In the example, the text you click to get is printed out with Log. You can see that the text cutting method here is only for spaces, so the words you get will have punctuation, etc. if you want to get strict words, you need to further process the obtained text.

Correction: the original integer [] indexes = getindexes (textview. Gettext(). Tostring(). Trim() + "", '');

Change to integer [] indexes = getindexes (textview. Gettext(). Tostring(). Trim(), ');

Published 201 original articles, praised 93, visited 120000+
Private letter follow

Tags: Attribute Android

Posted on Thu, 12 Mar 2020 01:21:43 -0700 by peranha