核心方法:
1. EditText监听文本变化:addTextChangedListener()
2. adapter收到文本变化之后,刷新listview notifyDataSetChanged()
下面的Demo是列举了知识点,然后通过输入知识点内容的关键字,然后在ListView中高亮显示
package com.csti.datastructureteachingsystem.module;
import cn.bmob.v3.BmobObject;
public class Knowledge extends BmobObject {
String Title;
String knowledge;
public Knowledge() {
}
public Knowledge(String tile, String knowledge) {
this.Title = tile;
this.knowledge = knowledge;
}
public String getKnowledge() {
return knowledge;
}
public void setKnowledge(String knowledge) {
this.knowledge = knowledge;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.KnowledgeList">
<ListView
android:id="@+id/knowledgeList"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/find" />
<EditText
android:id="@+id/find"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="搜索"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
package com.csti.datastructureteachingsystem.helper;
import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.csti.datastructureteachingsystem.R;
import com.csti.datastructureteachingsystem.module.Knowledge;
import java.util.List;
public class KnowledgeAdapter extends ArrayAdapter<Knowledge> {
private int resourceId;
private String changeStr="";
private List<Knowledge> mlist;
public KnowledgeAdapter(Context context, int resource, List<Knowledge> objects) {
super(context, resource, objects);
resourceId = resource;
mlist=objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Knowledge knowledge= getItem(position);
View view;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
} else {
view = convertView;
}
TextView textView=view.findViewById(R.id.item_tilte);
TextView textView1=view.findViewById(R.id.knowledge);
textView.setText(knowledge.getTitle());
textView1.setText(knowledge.getKnowledge());
//处理关键字的颜色变化
if(null!=mlist.get(position)&&mlist.get(position).getKnowledge().contains(changeStr)){
int index=mlist.get(position).getKnowledge().indexOf(changeStr);
int len=changeStr.length();
Spanned temp= Html.fromHtml(mlist.get(position).getKnowledge().substring(0,index)+""
+mlist.get(position).getKnowledge().substring(index,index+len)+""
+mlist.get(position).getKnowledge().substring(index+len,mlist.get(position).getKnowledge().length()));
textView1.setText(temp);
}else{
textView1.setText(mlist.get(position).getKnowledge());
}
return view;
}
//editext监听文本变化需要用到
public void changeText(String textstr){
this.changeStr=textstr;
//记得调用notifyDataSetChanged()方法刷新item的内容
notifyDataSetChanged();
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/photo"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/item_tilte"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/knowledge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"//单行显示
android:maxEms="11"//最多显示11个字符
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
package com.csti.datastructureteachingsystem.activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.ListView;
import com.csti.datastructureteachingsystem.R;
import com.csti.datastructureteachingsystem.helper.KnowledgeAdapter;
import com.csti.datastructureteachingsystem.module.Knowledge;
import com.csti.datastructureteachingsystem.module.Post;
import com.csti.datastructureteachingsystem.module.User;
import java.util.ArrayList;
import java.util.List;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
public class KnowledgeList extends AppCompatActivity {
static List<Knowledge> mlist = new ArrayList<>();
ListView listView;
EditText find;
KnowledgeAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_knowledge_list);
listView = findViewById(R.id.knowledgeList);
find=findViewById(R.id.find);
adapter=new KnowledgeAdapter(KnowledgeList.this,R.layout.knowledge_item,initData());
listView.setAdapter(adapter);
find.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.changeText(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private List<Knowledge> initData() {
List<Knowledge> mlistdata = new ArrayList<>();
mlistdata.add(new Knowledge("C语言","C语言是一门面向过程、抽象化的通用程序设计语言,广泛应用于底层开发。" +
"C语言能以简易的方式编译、处理低级存储器。C语言是仅产生少量的机器语言以及不需要任何运行环境支持便能运行的高效率程序设计语言。" +
"尽管C语言提供了许多低级处理的功能,但仍然保持着跨平台的特性,以一个标准规格写出的C语言程序可在包括一些类似嵌入式" +
"处理器以及超级计算机等作业平台的许多计算机平台上进行编译。"));
mlistdata.add(new Knowledge("Java","Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、" +
"指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以" +
"优雅的思维方式进行复杂的编程"));
mlistdata.add(new Knowledge("Python","Python是一种计算机程序设计语言。是一种面向对象的动态类型语言,最初被设计用于编写自动化脚本(shell)," +
"随着版本的不断更新和语言新功能的添加,越来越多被用于独立的、大型项目的开发。"));
return mlistdata;
}
}