TextView 中文本內容過長,需要快速定位到某關鍵字時,類似於WPS全文查找搜索內容,並且可以進行定位到搜索內容進行顯示,也可點擊按鈕,跳轉到下一處搜索內容處。
TextView進行關鍵字匹配,需要找到該查找內容在文本中的下標
private List<Integer> position;//全文中查找內容的所有位置
private int subPos = 0;
/**
* 獲取查找文字下標
* @param str 文本內容
* @param inputs 查找內容
* @param resStr 重新拼裝的文本內容
* @return
*/
public StringBuffer getSreachPosition(String str, String inputs, StringBuffer resStr) {
int index = str.length();//用來做爲標識,判斷關鍵字的下標
int theIndex = str.indexOf(inputs);
if (theIndex < index && theIndex > 0) {
index = theIndex;//替換下標
}
//如果條件成立,表示串中已經沒有可以被替換的關鍵字,否則遞歸處理
if (index == str.length()) {
resStr.append(str);
} else {
resStr.append(str.substring(0, index));
resStr.append("<font color='#FF0000'>" + str.substring(index, index + inputs.length()) + "</font>");
position.add(index + inputs.length() + subPos);
subPos += (index + inputs.length());
String str1 = str.substring(index + inputs.length(), str.length());
addChild(str1, inputs, resStr);//剩餘的字符串繼續替換
}
return resStr;
}
獲取到在TextView文本中的查找內容的所有位置後,通過這些位置獲取相應的Y軸座標,在TextView是通過Layout來管理字符的位置
private List<Integer> meau;// y 座標集合
/**
* 獲取每一個下標對應的 y 座標
* @param position
*/
public void getLine(List<Integer> position) {
Layout layout = textview.getLayout();
int yAxisBottom = 0;
for (int j = 0; j < position.size(); j++) {
Rect bound = new Rect();
int line = layout.getLineForOffset(position.get(j));
layout.getLineBounds(line, bound);
if (yAxisBottom != bound.bottom + textview.getScrollY()) {
yAxisBottom = bound.bottom + textview.getScrollY();
//這裏設置 y-100 是由於查找內容顯示到了屏幕最頂端,視覺效果不佳
if (yAxisBottom > 100) {
meau.add(yAxisBottom - 100);
}
}
}
}
TextView.getScrollY()獲取textview展示的內容最上面一行的座標,這樣的話我們就可以算出當前字符串的在屏幕上的座標位置。
然後在TextView上顯示出
/**
* @param content 顯示在TextView中的全文內容
*/
public void showContentSreach(String content) {
String input = edittext.getText().toString();
if (input.equals("")) {
ToastUtils.showToast(LawDetailsActivity.this, "查找內容不能爲空!");
} else {
StringBuffer resStr = new StringBuffer();
position = new ArrayList<>();
meau = new ArrayList<>();
subPos = 0;
String text = getSreachPosition(content, input, resStr).toString();
content.setText(Html.fromHtml(text));
if (null != position && position.size() > 0) {
getLine(position);
pos = 0;
scrollview.scrollTo(0, meau.get(pos));
}
}
}
接下來就是點擊按鈕可以快速定位到下一個地方
private int pos = 0; //記錄點擊次數
@OnClick({R.id.syg, R.id.xyg})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.syg:
if (null != meau && meau.size() > 0) {
if (pos == 0){
ToastUtils.showToast(this, "已到到頂了");
} else {
pos --;
scrollview.scrollTo(0, meau.get(pos));
}
}
break;
case R.id.xyg:
if (null != meau && meau.size() > 0) {
if (pos == meau.size()){
ToastUtils.showToast(this, "已到到底了");
} else {
scrollview.scrollTo(0, meau.get(pos));
pos ++;
}
}
break;
}
}
最後貼上我的佈局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<ScrollerView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="@color/black_text01"
android:layout_marginBottom="10dp"/>
</LinearLayout>
</ScrollerView>
<LinearLayout
android:id="@+id/sreach_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:background="#50000000"
android:padding="7dp"
android:layout_below="@+id/title_layout">
<EditText
android:id="@+id/edit_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/edit_background"
android:textColor="@color/text_color"
android:textSize="17sp"
android:padding="7dp"/>
<Button
android:id="@+id/query"
android:layout_width="60dp"
android:layout_height="@dimen/dp_35"
android:background="@drawable/ic_button_blue"
android:layout_marginLeft="10dp"
android:text="查找"
android:textColor="@color/white"
android:textSize="@dimen/sp_17"/>
</LinearLayout>
<LinearLayout
android:id="@+id/sreach_tools"
android:layout_width="match_parent"
android:layout_height="35dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_margin="30dp"
android:layout_alignParentBottom="true"
android:background="@drawable/text_style_02">
<ImageView
android:id="@+id/syg"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:gravity="center"
android:src="@drawable/syg"/>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#b9b9b9"/>
<ImageView
android:id="@+id/xyg"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:gravity="center"
android:src="@drawable/xyg"/>
</LinearLayout>
</RelativeLayout>