简体   繁体   中英

how to do highlight on Strings

I only want to find the words on the Diccionario Array and highlight but show other words to Example: If I search "spannable", i want to return the same text but with the words who find it on highlight or other color !

在此处输入图片说明

public class FindString extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_string);

        final Button btnSearch = (Button) findViewById(R.id.searchBtn);
        final EditText Searchtxt = (EditText)findViewById(R.id.textSearch);
        final String[] Diccionario = {"hola","adios","ayer","hoy","mañana"};

btnSearch.setOnClickListener(new View.OnClickListener() {                                       


            public void onClick(View v){
                    String FinalText = " " ;
                    String SearchText ="";                                                          
                    SearchText = Searchtxt.getText().toString();

                                        String[] split = SearchText.split(" "); 

                    for(int i=0; i<split.length; i++)  
                    { 
                        for(int j=0; j<Diccionario.length;j++)
                            {
                            if(split[i].equals(Diccionario[j]))
                                    {   
                               FinalText = FinalText +" "+split[i];         
                                    }   
                            }
                    }

                    }

emphasized text

try this

{
       final SpannableStringBuilder sb = new SpannableStringBuilder(FinalText);
       final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(158, 158, 158)); 

       // Span to set text color to some RGB value
       final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); 

       // Span to make text bold
       sb.setSpan(fcs, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 

       // Set the text color for first 4 characters
       sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); 

       // make them also bold
       yourTextView.setText(sb);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM