效果图如下:
关键字搜索不区分大小写,TextView换成EditText也同样适用。
Activity部局代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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=".MainActivity"> <EditText android:id="@+id/edtKeyword" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:ems="10" android:inputType="textPersonName" app:layout_constraintEnd_toStartOf="@+id/btnSearch" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btnSearch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:text="搜索" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/tvContent" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginStart="8dp" android:layout_marginLeft="8dp" android:layout_marginTop="8dp" android:layout_marginEnd="8dp" android:layout_marginRight="8dp" android:layout_marginBottom="8dp" android:ems="10" android:gravity="start|top" android:inputType="textMultiLine" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/edtKeyword" /> </androidx.constraintlayout.widget.ConstraintLayout> |
Activity代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
package com.example.highlightstrapp; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.text.SpannableString; import android.text.Spanned; import android.text.style.BackgroundColorSpan; import android.text.style.ForegroundColorSpan; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private EditText edtKeyword; private TextView tvContent; private Button btnSearch; private String mContent = " There are moments in life when you miss someone so much that you " + "just want to pick them from your dreams and hug them for real! Dream what you want to " + "dream;go where you want to go;be what you want to be,because you have only one life " + "and one chance to do all the things you want to do.\n" + "\n" + " May you have enough happiness to make you sweet,enough trials to make you strong," + "enough sorrow to keep you human,enough hope to make you happy? Always put yourself " + "in others’shoes.If you feel that it hurts you,it probably hurts the other person, too.\n" + "\n" + " The happiest of people don’t necessarily have the best of everything;they just " + "make the most of everything that comes along their way.Happiness lies for those who " + "cry,those who hurt, those who have searched,and those who have tried,for only they " + "can appreciate the importance of people"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); initEvents(); } private void initEvents() { btnSearch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { hightStr(edtKeyword.getText().toString(), mContent); } }); } private void initViews() { tvContent = findViewById(R.id.tvContent); edtKeyword = findViewById(R.id.edtKeyword); btnSearch = findViewById(R.id.btnSearch); } public void hightStr(String hlStr, String srcString){ tvContent.setText(""); String hlLower = hlStr.toLowerCase(); int lastEnd = 0; SpannableString spannableString = new SpannableString(srcString); for(int i = 0; i <= srcString.length() - hlStr.length(); i++){ if(srcString.substring(i, i + hlStr.length()).toLowerCase().equals(hlLower)){ spannableString.setSpan(new ForegroundColorSpan(Color.RED), i, i + hlStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); spannableString.setSpan(new BackgroundColorSpan(Color.YELLOW), i, i + hlStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } tvContent.append(spannableString); } } |