搜索關鍵字變顏色

相信大家平時都看到很多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<Integer> sTextsStartList = new ArrayList<Integer>();

    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代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    
    <EditText 
        android:id="@+id/search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="請輸入關鍵字"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="搜索" />

    <com.bawei.col.view.MyTextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
三、現在佈局也完成了,可以進行搜索關鍵字查找了

代碼如下:(這裏我是模擬的死數據,相信同學看完就知道怎麼使用了)

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. 完成上述代碼就可以實現搜索關鍵字變色的效果哦。

效果圖如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章