我已在我的应用中实施了 SearchView
小工具。它的工作正常。现在我需要做的是,每当我在 SearchView Bar
中输入一个单词时,过滤后的结果应该会突出显示搜索到的单词。喜欢:
我正在使用 SearchView 小部件:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.myMenu , menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView sv = new SearchView(getActivity());
// Changing the color of Searchview widget text field to white.
int searchSrcTextId = getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchEditText = (EditText) sv.findViewById(searchSrcTextId);
searchEditText.setTextColor(Color.WHITE);
sv.setOnQueryTextListener(this);
searchItem.setActionView(sv);
super.onCreateOptionsMenu(menu, inflater);
}
答案 0 :(得分:5)
您可以使用Spannable TextView。 希望这个方法能帮到你
public static CharSequence highlightText(String search, String originalText) {
if (search != null && !search.equalsIgnoreCase("")) {
String normalizedText = Normalizer.normalize(originalText, Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "").toLowerCase();
int start = normalizedText.indexOf(search);
if (start < 0) {
return originalText;
} else {
Spannable highlighted = new SpannableString(originalText);
while (start >= 0) {
int spanStart = Math.min(start, originalText.length());
int spanEnd = Math.min(start + search.length(), originalText.length());
highlighted.setSpan(new ForegroundColorSpan(Color.BLUE), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
start = normalizedText.indexOf(search, spanEnd);
}
return highlighted;
}
}
return originalText;
}
和return originalText
会突出显示文字。
答案 1 :(得分:4)
您应该在onBindViewHolder()方法(使用RecyclerView)
中执行此操作类YourAdapter
String searchString="";
@Override
public void onBindViewHolder(AllMessageAdapter.DataObjectHolder holder, final int position) {
holder.message.setText(mDataset.get(position).Message);
AllMessageList.Message message=mDataset.get(position);
String name = message.Message.toLowerCase(Locale.getDefault());
if (name.contains(searchString)) {
int startPos = name.indexOf(searchString);
int endPos = startPos + searchString.length();
Spannable spanString = Spannable.Factory.getInstance().newSpannable(holder.message.getText());
spanString.setSpan(new ForegroundColorSpan(Color.RED), startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.message.setText(spanString);
}
}
您的过滤器(在适配器中)
public void setFilter(ArrayList<AllMessageList.Message> countryModels,String searchString) {
this.searchString=searchString;
mDataset = new ArrayList<>();
mDataset.addAll(countryModels);
notifyDataSetChanged();
}
答案 2 :(得分:2)
您可以使用它来突出显示所有关键字。
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String ett = edittext.getText().toString();
String tvt = textview.getText().toString();
int ofe = tvt.indexOf(ett, 0);
Spannable spannable = new SpannableString(tvt);
for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
ofe = tvt.indexOf(ett, ofs);
if (ofe == -1)
break;
else {
ColorStateList blueColor = new ColorStateList(new nt[][] { new int[] {} }, new int[] { Color.BLUE });
TextAppearanceSpan highlightSpan = new TextAppearanceSpan(null, Typeface.BOLD, -1, blueColor, null);
spannable.setSpan(highlightSpan, ofe, ofe+edittext.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(spannable);
}
}
}
});