如何突出字符串

时间:2013-12-01 09:45:32

标签: java android arrays colors textview

我只想找到Diccionario数组上的单词并突出显示但是显示其他单词 示例:如果我搜索“spannable”,我想返回相同的文本,但是在高亮或其他颜色上找到它的单词!

enter image description here

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

                    }

强调文字

1 个答案:

答案 0 :(得分:0)

试试这个

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