EditText输入文本,ListView中过滤文字并上色

   我在CSDN上看到好多过滤文字的,却找不到过滤并上色的文章,所以今天我就汇集大家的聪明才智写出这篇,与此同时加深自己的印象,也能让大家了解下。先看下方图片的效果(o(∩_∩)o 我终于知道怎么在CSDN中贴图了):

 

代码----

MainActivity.class类:

package com.example.autocomplete;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
 private List<People> people = new ArrayList<People>();
 private Adapter adapter;
 private EditText eidtText;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  eidtText = (EditText) findViewById(R.id.edit);
  ListView list = (ListView) findViewById(R.id.list);
  initData();
  adapter = new Adapter(people);
  list.setAdapter(adapter);
  eidtText.addTextChangedListener(new TextWatcher() {

   @Override
   public void onTextChanged(CharSequence s, int start, int before,
     int count) {
    // TODO Auto-generated method stub
    String word = s.toString();
    Pattern pattern = Pattern.compile(word.toLowerCase());
    List<People> pList = new ArrayList<People>();
    for (int i = 0; i < people.size(); i++) {
     People peo = people.get(i);
     String infoString = peo.getPhome();
     Matcher matcher = pattern.matcher(infoString.toLowerCase());
     
     
     if (matcher.find()) {
      pList.add(peo);// 添入那些含有该文字或者该连续文字的数据。

     }
    }
    adapter.changeList(pList);
   }

   @Override
   public void beforeTextChanged(CharSequence s, int start, int count,
     int after) {
    // TODO Auto-generated method stub

   }

   @Override
   public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub

   }
  });
 }

 void initData() {
  people.add(new People("A15240672486"));
  people.add(new People("B10000672486"));
  people.add(new People("c10222672576"));
  people.add(new People("c13333692486"));
  people.add(new People("abcdE17777672555"));
  people.add(new People("Fe10111672486"));

 }

 public class Adapter extends BaseAdapter {
  List<People> people;

  void changeList(List<People> people) {
   this.people = people;
   notifyDataSetChanged();
  }

  public Adapter(List<People> people) {
   // TODO Auto-generated constructor stub
   this.people = people;
  }

  @Override
  public int getCount() {
   // TODO Auto-generated method stub
   return people.size();
  }

  @Override
  public Object getItem(int position) {
   // TODO Auto-generated method stub
   return null;
  }

  @Override
  public long getItemId(int position) {
   // TODO Auto-generated method stub
   return 0;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   // TODO Auto-generated method stub
   if (convertView == null) {
    convertView = getLayoutInflater().inflate(
      R.layout.item_content, null);
   }
   /*
    * 当调用changeList()方法的时候,getView将按照更新后的数据,一条条配置到ListView中,
    * 当在editText这个View中输入到某个文字时
    * 因为每次循环近来配置item,则会逐条检查该输入字在这个这串phoneString中的位置
    */

   String text = eidtText.getText().toString();
   String phoneString = people.get(position).getPhome();

   TextView wordView = (TextView) convertView.findViewById(R.id.word);
   int index = phoneString.toLowerCase().indexOf(text.toLowerCase());
   int length = text.length();
   Spanned temp = Html.fromHtml(phoneString.substring(0, index)
     + "<u><font color=#FF0000>"
     + phoneString.substring(index, index + length)
     + "</font></u>"
     + phoneString.substring(index + length,
       phoneString.length()));

   wordView.setText(temp);

   return convertView;
  }

 }

}

 

people.class类:

package com.example.autocomplete;

public class People {

 private String Phome;

 public String getPhome() {
  return Phome;
 }

 public void setPhome(String phome) {
  Phome = phome;
 }

 public People(String phone) {
  super();

  this.Phome = phone;
 }
}

 

XML文件:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</LinearLayout>

 

item_content.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/word"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="2343" />

</LinearLayout>

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值