简体   繁体   English

换行符(\\n 或 br)使用被 html 忽略的字符串数组项

[英]line break (\n or br)using string-array item ignored by html

SOLVED code updated已解决代码已更新

when I write a text on edit text to be searched on textview it highlights the particular text in colour but the line breaks are all gone and all my sentence appear in one sentence.And this problem appears when I click search for text to be highlighted.Else before I click search it shows all the line breaks.Please help!upvote waiting当我在要在 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"/>

//for example before i input text to search //例如在我输入文本进行搜索之前

"this is first test" “这是第一次测试”

//but after I input text to search it highlights particular text but shows without new line //但在我输入文本进行搜索后,它会突出显示特定文本但没有换行显示

"this is first test" “这是第一次测试”

//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;
            }
        }

New edit新编辑

Try a new one,尝试一个新的,

Use this as it is按原样使用它

// string code
<string-array name="chapters">
    <item>This is \n first test<item>
    <item>this is \n second test<item>
</string-array>

And,和,

@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;
        }
    }
}

Try this sample code试试这个示例代码

MainActivity.java主活动.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 *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>

strings.xml字符串.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>

Hope it will work out :)希望它会成功:)

//first output //第一个输出

This is <br /> first test

//Now when i enter text to search it shows correctly //现在当我输入文本进行搜索时,它显示正确

This is
first test

//Now if i enter another text to search it ignores the line break again //现在,如果我输入另一个文本进行搜索,它会再次忽略换行符

This is first test
// using this solved
Spanned htmlText = Html.fromHtml(text);
textView.setText(htmlText);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM