我有一个字符串java
,这是我在textview中查找或查找的内容。如果我搜索该字符串,它会使用我已经使用过的函数突出显示该单词,并且它工作正常。现在,我现在要做的是转到等同于字符串next
的{{1}}或previous
字。我怎样才能实现这样的目标?
这是我到目前为止所尝试的内容:
java
在textview中突出显示字符串的函数
TextView details = (TextView) findViewById(R.id.details);
details.setText(text.getDetails());
//value of string to find
TextView text = (TextView) findViewById(R.id.textview);
String getText = text.getText().toString();
changeTextView(details, getText);
我想在这里实现这样的形象:
答案 0 :(得分:0)
试试这个。要设置突出显示,请使用BackgroundColorSpan。
int index=0;
TextView tv ;
private void changeTextView(String target,boolean prev)
{
target = target.toLowerCase();
String bString = tv.getText().toString();
int startSpan = 0, endSpan = 0;
Spannable spanRange = new SpannableString(bString);
bString = bString.toLowerCase();
if(prev)
{
index -= target.length();
index -= 1;
if(index <= -1)
index = bString.length()-1;
index = bString.lastIndexOf(target, index);
if(index <= -1)
index = bString.lastIndexOf(target, bString.length()-1);
index += target.length();
}
else
{
index = bString.indexOf(target, index);
if(index == -1)
index = bString.indexOf(target, 0);
index += target.length();
}
while(true) {
startSpan = bString.indexOf(target, endSpan);
if(startSpan < 0)
break;
endSpan = startSpan + target.length();
if(index == endSpan)
spanRange.setSpan(
new BackgroundColorSpan(Color.BLUE),
startSpan,
endSpan,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
else
spanRange.setSpan(
new BackgroundColorSpan(Color.YELLOW),
startSpan,
endSpan,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tv.setText(spanRange);
}
OnCreate方法
tv = ((TextView) findViewById(R.id.textView1));
changeTextView("search_text",false);
//Previous
((Button) findViewById(R.id.prev)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
changeTextView("find",true);
}
});
//next
((Button) findViewById(R.id.next)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
changeTextView("find",false);
}
});
答案 1 :(得分:0)
我可以给你一个提示,仔细想想,你可以得到答案。
将 paragrapgh 的内容存储在字符串中。
在while循环之外将变量count
初始化为0。
在while循环中,如果能够找到你感兴趣的字符串,那么继续将count
变量的值递增1,之后调用另一个while循环
while(count!=0)
{
spanRange.setSpan(
new StyleSpan(android.graphics.Typeface.BOLD),
startSpan,
endSpan,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//function which higlights the word
count--;
}
此计数变量将跟踪段落中出现的字符串。
通过这种方法,您将跟踪给定段落中的所有单词,并仅突出显示您感兴趣的字符串。
答案 2 :(得分:0)
您应该管理所选文本的索引。按next键增加此值,按前一键减小。然后选择跨度取决于所选索引。
将此点击侦听器添加到按钮
findViewById(R.id.next).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selected++;
}
});
findViewById(R.id.previous).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selected--;
}
});
将方法调用更改为此
selected = changeTextView(details, getText, selected);
并使用此更新方法
/**
* Returns normalized value, if selected index out of bounds
*
* @param tv textView to display search results
* @param target text to search
* @param selected int selected index
* @return normalized value of selected item or -1 if text not found
*/
private int changeTextView(TextView tv, String target, int selected) {
if (selected < 0) {
selected = 0;
}
String bString = tv.getText().toString();
int startSpan = 0, endSpan = 0;
Spannable spanRange = new SpannableString(bString);
int currentIndex = 0;
while (true) {
startSpan = bString.indexOf(target, endSpan);
endSpan = startSpan + target.length();
boolean isLast = bString.indexOf(target, endSpan) < 0;
if (startSpan < 0)
break;
ParcelableSpan span;
if (currentIndex == selected || isLast && selected > currentIndex) {
// selected span
span = new StyleSpan(Typeface.BOLD_ITALIC);
if (isLast && selected > currentIndex) {
// normalize if selected out of bounds
selected = currentIndex;
}
} else {
// deselected span
span = new StyleSpan(Typeface.BOLD);
}
currentIndex++;
spanRange.setSpan(
span,
startSpan,
endSpan,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tv.setText(spanRange);
if (currentIndex == 0) {
// text not found
return -1;
} else {
return selected;
}
}