[英]line break (\n or br)using string-array item ignored by html
当我在要在 textview 上搜索的编辑文本上编写文本时,它会以颜色突出显示特定文本,但换行符都消失了,我的所有句子都出现在一个句子中。当我单击搜索要突出显示的文本时,就会出现这个问题。否则,在我单击搜索之前,它会显示所有换行符。请帮忙!upvote 等待
// 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;
}
}
新编辑
尝试一个新的,
按原样使用它
// string code
<string-array name="chapters">
<item>This is \n first test<item>
<item>this is \n second test<item>
</string-array>
和,
@Override
public void onClick(View v) {
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;
}
}
}
试试这个示例代码
主活动.java
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
setContentView(R.layout.activity_main);
final EditText edt = findViewById(R.id.edt);
final TextView tv = findViewById(R.id.tv);
Button btn = findViewById(R.id.btn);
tv.setText(Html.fromHtml(getResources().getStringArray(R.array.chapters)[0].replaceAll("\n", "<br />")), TextView.BufferType.SPANNABLE);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String textTOHighlight = edt.getText().toString();
String originalText = tv.getText().toString();
if(originalText.isEmpty()) return;
String modifiedText = originalText.replaceAll("\n", "<br />").replaceAll(textTOHighlight, "<font color='red'>" + textTOHighlight + "</font>");
tv.setText(Html.fromHtml(modifiedText),TextView.BufferType.SPANNABLE);
}
});
}
}
*activity_main.xml
<?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:orientation="vertical">
<EditText
android:id="@+id/edt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:hint="Enter text"
android:padding="7dp"
android:textColor="#000000"
android:textSize="16sp" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edt"
android:layout_marginTop="10dp"
android:background="#C29090"
android:text="Search" />
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/btn"
android:layout_marginTop="90dp"
android:background="#474747"
android:scrollbars="vertical"
android:textColor="#ffffff"
android:textSize="16sp" />
</RelativeLayout>
字符串.xml
<resources>
<string name="app_name">My Application</string>
<string-array name="chapters">
<item>This is \n first test</item>
<item>this is \n second test</item>
</string-array>
</resources>
希望它会成功:)
//第一个输出
This is <br /> first test
//现在当我输入文本进行搜索时,它显示正确
This is
first test
//现在,如果我输入另一个文本进行搜索,它会再次忽略换行符
This is first test
// using this solved
Spanned htmlText = Html.fromHtml(text);
textView.setText(htmlText);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.