替换字符串文本?

时间:2014-09-29 10:48:24

标签: android arrays string text replace

我正在尝试开发一个由多个数组和textview组成的应用程序,但我遇到了一个问题。

我有3个textviews和2个数组。

我的一个数组包含句子。 我的第一个问题是如何突出每个句子中的特定单词? 例如:“这是我的第一个数组项目” 我需要在字符串中突出显示一个单词,这样当它在textview1中显示时,它将显示为这样...... “这是我的第一个数组项目”

我的其他数组包含单词。它们显示在textview2中,也应突出显示。 我的下一个问题是如何用textview2中新突出显示的单词替换textview1中当前突出显示的单词,还有第三个文本视图显示新句子? 例如:如果从我的句子数组中选择的项目是“这是测试”(显示在textview1中)并且我的单词数组中的项目是“网站”(显示在textview2)结果将是“这是一个网站”(显示在textview3中)

到目前为止,这是我的代码

import java.util.Random;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Button;



public class Class1 extends Activity implements OnClickListener
    {

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

        Button btn_sentence = (Button) findViewById(R.id.btn_sentence );
        Button btn_word = (Button) findViewById(R.id.btn_word );
        Button btn_newsentence = (Button) findViewById(R.id.btn_newsentence );

        final Random ran = new Random();
        final Resources res = getResources();
        final TextView text1 = (TextView) findViewById(R.id.sentencetext);
        final TextView text2 = (TextView) findViewById(R.id.wordtext);
        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(strRandom);

            }
            });
        btn_word.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

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

            }
            });
        btn_newsentence.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                **text3.setText(" " + (text1.getText()) + " " + (text2.getText()) + " ");**

            }
            });

    }


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

    }}

粗体文字和我一样接近,但真正做的就是将TexttView1和textview2结合起来,并在textview3中显示...这不是我想要的。

非常感谢帮助      〜TylerNormal

1 个答案:

答案 0 :(得分:1)

您可以使用简单的HTML标记,将要突出显示的单词包装成使用此标记的文字:

TextView text = findViewByID(R.id.myTextView);
String test = "This is my first array item";
// example - the word in brackets is to be replaced and captured
// it is just an example to show how you can use it later with more
// complex cases
test = test.replaceAll("(first)", "<b>$1</b>");
text.setText(Html.fromHtml(test));
相关问题