【问题标题】:line break (\n or br)using string-array item ignored by html使用 html 忽略的字符串数组项的换行符(\n 或 br)
【发布时间】: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;
            }
        }

【问题讨论】:

    标签: java android html string


    【解决方案1】:

    新编辑

    尝试一个新的,

    按原样使用

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

    试试这个示例代码

    MainActivity.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>
    

    strings.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>
    

    希望它会成功:)

    【讨论】:

    • 问题必须出在 html 上,当我用高亮替换文本时,它会忽略新的行标签。也许我错了
    • 如果是,请替换
      而不是 \n
    • 你能告诉我插入一个叫做chions的字符串数组的代码吗?该问题没有该代码。
    • 请应用,因为我刚刚修改过。
    • 我使用了你的代码,当我构建它时显示如下。“这是
      第一次测试”。现在在搜索按钮上,当我点击文本进行搜索时......它显示了正确的结果换行符。现在如果我再次搜索,换行符将被忽略。我希望你能理解。你能自己试试吗。太棘手了还是我应该在没有 html 的情况下实现。
    【解决方案2】:

    //第一个输出

    This is <br /> first test
    

    //现在当我输入文本进行搜索时,它会正确显示

    This is
    first test
    

    //现在如果我输入另一个文本进行搜索,它会再次忽略换行符

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

    【讨论】:

      猜你喜欢
      • 2020-08-08
      • 2010-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 2015-07-01
      • 1970-01-01
      • 2019-03-13
      相关资源
      最近更新 更多