提问人:Mohd Saadi 提问时间:8/8/2023 更新时间:8/8/2023 访问量:20
使用变音符号突出显示字符串中的阿拉伯单词 Android
highlight an arabic word in a string with diacritics Android
问:
我想在包含变音符号的字符串中突出显示搜索到的单词, 用户搜索单词示例“محمد”,因此我实现了一个函数,该函数从字符串中删除变音符号并返回字符串中搜索到的单词的索引 带变音符号的字符串 : حَدَّثَنَا مُحَمَّدُ 不带变音符号的字符串 : حدثنا محمد 这里单词的起始索引是 6 但问题是搜索到的单词的索引在带有变音符号的内容中并不相同
这是检索搜索单词的索引并应用突出显示的方法
String normalizedSearchText = removeDiacritics(searchText.toLowerCase().trim());
String normalizedContent = removeDiacritics(content.toLowerCase().trim());
int startIndex = normalizedContent.indexOf(normalizedSearchText);
if (startIndex != -1) {
Spannable spannable = new SpannableString(content);
int endIndex = startIndex + searchText.length();
// Customize the background color of the matched text
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.YELLOW);
spannable.setSpan(backgroundColorSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Apply the Spannable to the TextView
binding.tvContent.setText(spannable);
这是搜索到的单词是第二个单词的输出
答: 暂无答案
评论