搜索关键字变颜色

相信大家平时都看到很多app上都有这个功能,所搜的关键字会变成红色或者别的颜色,但是android的TextView却没有这个属性。现在小编就简单介绍下。

一、首先,我们知道android自带的TextView 没有这个功能,那么我们可以自定义TextView来实现这个功能。

自定义TextView代码如下:

package com.bawei.col.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.widget.TextView;

public class MyTextView extends TextView {

public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void setSpecifiedTextsColor(String text, String specifiedTexts, int color) {
    List sTextsStartList = new ArrayList();

    int sTextLength = specifiedTexts.length();
    String temp = text;
    int lengthFront = 0;//记录被找出后前面的字段的长度
    int start = -1;
    do {
        start = temp.indexOf(specifiedTexts);

        if (start != -1) {
            start = start + lengthFront;
            sTextsStartList.add(start);
            lengthFront = start + sTextLength;
            temp = text.substring(lengthFront);
        }

    } while (start != -1);

    SpannableStringBuilder styledText = new SpannableStringBuilder(text);
    for (Integer i : sTextsStartList) {
        styledText.setSpan(
                new ForegroundColorSpan(color),
                i,
                i + sTextLength,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    setText(styledText);
}
}

现在我们有了自定义的这个TextView控件,我们只需要在布局中使用它就行了

布局Xml代码如下:


    
    

    
三、现在布局也完成了,可以进行搜索关键字查找了

代码如下:(这里我是模拟的死数据,相信同学看完就知道怎么使用了)

package com.bawei.col;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.bawei.col.view.MyTextView;

public class MainActivity extends Activity {

    private EditText search;
	private MyTextView text;
	public String result = "明朝年间,有一个叫谷才的人,他收了很多个弟子,形成了一个集团,都男扮女装,以教授妇女手工活,晚上在把女子那个,当然,这代表谷才的化妆技术真的非常好,让人家真的以为是女子,而谷才有一个弟子,叫桑冲,一次,他又去行骗,以怕被老公打来借宿之名,住在晋州聂村生员高宣家,刚好高宣有个女婿,叫赵文举,看上了这个男扮女装的桑冲,而桑冲呢,是看中高家小姐,晚上正在打高家小姐的主意时,竟反被赵文举给压住,想对他那个,结果事情才这么东窗事发,桑冲当场被送去官府,据桑冲的口供,他已经犯案成功了182次了!!";

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //找控件
        Button button = (Button) findViewById(R.id.button);//搜索按钮
        search = (EditText) findViewById(R.id.search);//输入框
        text = (MyTextView) findViewById(R.id.text);//文本
        
        //设置按钮监听
        button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//获取文本框的内容
				String trim = search.getText().toString().trim();
				//设置关键字颜色
				/**
				 * result 搜索到的内容
				 * trim 需要改变颜色的关键字
				 */
				text.setSpecifiedTextsColor(result, trim, Color.parseColor("#FF0000"));
			}
		});
    }
}

  1. 完成上述代码就可以实现搜索关键字变色的效果哦。

效果图如下:

你可能感兴趣的:(自定义控件)