从textview替换粗体文本?

时间:2014-10-02 19:32:54

标签: java android string replace bold

有没有办法将我的数组中的粗体文本带到我的第三个textview?我已经通过对每个文本视图执行按钮单击,将其从XML转移到我的第一和第二个文本视图。我试图让我的第三个textview用粗体动物替换句子中的粗体字。我的第三个textview将每秒不断显示新组合,以便每次单击按钮更改动物或句子时,textview将显示新组合。

这是我的strings.xml

<string-array name="Sentences">
        <item>"The big <![CDATA[<b>This</b>]]> ate the worm"</item>
        <item>"The <![CDATA[<b>Is</b>]]> is strong"</item>
        <item>"Every <![CDATA[<b>Just</b>]]> can run"</item>
        <item>"I saw a <![CDATA[<b>A</b>]]>"</item>
        <item>"The <![CDATA[<b>Test</b>]]> was sleeping"</item>
    </string-array>

    <string-array name="Animals">
        <item>"<![CDATA[<b>Cow</b>]]>"</item>
        <item>"<![CDATA[<b>Sheep</b>]]>"</item>
        <item>"<![CDATA[<b>Dog</b>]]>"</item>
        <item>"<![CDATA[<b>Pig</b>]]>"</item>
        <item>"<![CDATA[<b>Chicken</b>]]>"</item>
    </string-array>

这是我的main.java

package com.MyFirstApp.App1;

import java.util.Random;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Button;



public class Original extends Activity implements OnClickListener
    {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn_sentence = (Button) findViewById(R.id.btn_sentence );
        Button btn_animal = (Button) findViewById(R.id.btn_animal );

        final Random ran = new Random();
        final Resources res = getResources();
        final TextView text1 = (TextView) findViewById(R.id.sentencetext);
        final TextView text2 = (TextView) findViewById(R.id.animaltext);
        final TextView text3 = (TextView) findViewById(R.id.newsentencetext);


        btn_sentence .setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                String[] NewText = null;
                NewText = res.getStringArray(R.array.Sentences);
                String strRandom = NewText[ran.nextInt(NewText.length)];
                text1.setText(Html.fromHtml(strRandom));
            }
            });
        btn_animal .setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                String[] NewText2 = null;
                NewText2 = res.getStringArray(R.array.Animals);
                String strRandom2 = NewText2[ran.nextInt(NewText2.length)];
                text2.setText(Html.fromHtml(strRandom2));
            }
            });

        Thread t = new Thread() {

              @Override
              public void run() {
                try {
                  while (!isInterrupted()) {
                    Thread.sleep(1);
                    runOnUiThread(new Runnable() {
                      @Override
                      public void run() {



                          String str = Html.fromHtml(text1).toString();
                          String str2 = text2.getText().toString();
                          str = str.replaceAll("<![CDATA[<b></b>]]>", str2);
                          text3.setText(Html.fromHtml(str));

                      }
                    });
                  }
                } catch (InterruptedException e) {
                }
              }
            };

            t.start();}

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }}

这是我遇到问题的地方

 String str = Html.fromHtml(text1).toString();
 String str2 = text2.getText().toString();
 str = str.replaceAll("<![CDATA[<b></b>]]>", str2);
 text3.setText(Html.fromHtml(str));

我似乎无法从第一个textview或第二个textview获取粗体文本。 例如

`String str2 = text2.getText().toString();` 

只返回第二个textview中的文本,但它不像我需要的那样是粗体。解决这个问题之后,我希望我可以使用这一行

str = str.replaceAll("<![CDATA[<b></b>]]>", str2);

用新的粗体动物替换旧的粗体文本。

非常感谢帮助

 ~TylerNormal

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情来构建粗体混合字符序列

    TextView tv = (TextView) findViewById(R.id.txt);
    SpannableStringBuilder b = new SpannableStringBuilder();
    b.append("hello ");
    String boldData = "bold Data";
    int startix = b.length();
    int len = boldData.length();
    b.append(boldData);
    b.setSpan(new StyleSpan(Typeface.BOLD), startix, startix + len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    tv.setText(b);