首页 > 解决方案 > 突出显示文本视图中的搜索文本不起作用

问题描述

#solved#当我构建时没有错误,但是在编辑文本中搜索特定文本并点击搜索按钮没有进行任何更改,即我的文本上没有突出显示。请帮助。下面是我的代码

// my java code
public void onClick(View v) {
                String textTOHighlight=etText.getText().toString();
  //delete this line  String replacedWith = "<span style='background-color:yellow'>"+textTOHighlight+"</span>";
                String originalText=textView.getText().toString();
// delete this line  String modifiedText=originalText.replaceAll(textTOHighlight,replacedWith);
                textView.setText(Html.fromHtml(modifiedText));
//added this and it works
String modifiedText=originalText.replaceAll(textTOHighlight, "<font color='red'>"+textTOHighlight+"</font>");
                textView.setText(Html.fromHtml(modifiedText));
// strings.xml
<string-array name="chapters">
<item>this is just an example for first item</item>
<item>this is the second example <item>
</string-array>
// activity
 <EditText
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:id="@+id/et_text"
        android:hint="Enter text"
        android:padding="7dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/bg_round"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/search"
        android:text="Search"
        android:layout_marginTop="10dp"
        android:layout_marginStart="120dp"/>

    <TextView
        android:id="@+id/textv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="90dp"
        android:scrollbars="vertical"
        android:background="@drawable/bg_round"/>

标签: androidstring

解决方案


推荐阅读