【发布时间】:2020-03-04 07:07:45
【问题描述】:
已解决代码已更新
当我在要在 textview 上搜索的编辑文本上编写文本时,它会以颜色突出显示特定文本,但换行符都消失了,我的所有句子都出现在一个句子中。当我单击搜索文本时出现此问题被突出显示。否则在我点击搜索之前它会显示所有换行符。请帮助!等待投票
// java code
public void onClick(View v) {
//solved code here
String textTOHighlight=etText.getText().toString();
String originalText=textView.getText().toString();
if(originalText.isEmpty()) return;
String modifiedText=originalText.replaceAll("\n", "<br />").replaceAll(textTOHighlight, "<font color='red'>"+textTOHighlight+"</font>");
textView.setText(Html.fromHtml(modifiedText),TextView.BufferType.SPANNABLE);
}
// string code
<string-array name="chapters">
<item>This is \n first test<item>
<item>this is \n second test<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"/>
//例如在我输入文本进行搜索之前
“这是 第一次测试”
//但在我输入文本进行搜索后,它会突出显示特定文本,但显示时没有换行
“这是第一次测试”
//all i did was on each button click go to specific chapter.By the way this portion has nothing to do.
@Override
public void onClick(View v) {
//solved code
String text="";
switch(v.getId()){
case R.id.btn1 : {
text = getResources().getStringArray(R.array.chapters)[0].replaceAll("\n", "<br />");
break;
}
case R.id.button1 : {
text = getResources().getStringArray(R.array.chapters)[1].replaceAll("\n", "<br />");
break;
}
}
【问题讨论】: