Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 替换字符串文本?_Android_Arrays_String_Text_Replace - Fatal编程技术网

Android 替换字符串文本?

Android 替换字符串文本?,android,arrays,string,text,replace,Android,Arrays,String,Text,Replace,我正在尝试开发一个包含多个数组和文本视图的应用程序,但遇到了一个问题 我有3个文本视图和2个数组 我的一个数组包含句子。 我的第一个问题是如何突出每个句子中的特定单词? Ex:“这是我的第一个数组项” 我需要突出显示字符串中的一个单词,这样当它显示在textview1中时,它将显示如下。。。 “这是我的第一个数组项” 我的另一个数组包含单词。它们显示在textview2中,也应高亮显示。 我的下一个问题是,如何将textview1中当前突出显示的单词替换为textview2中新突出显示的单词,并

我正在尝试开发一个包含多个数组和文本视图的应用程序,但遇到了一个问题

我有3个文本视图和2个数组

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

我的另一个数组包含单词。它们显示在textview2中,也应高亮显示。 我的下一个问题是,如何将textview1中当前突出显示的单词替换为textview2中新突出显示的单词,并使第三个textview显示新句子? 例如:如果从我的句子数组中选择的项目是“这是一个测试”(显示在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

    }}
粗体文本和我得到的一样接近,但真正做的是将textVIEW1和textview2结合起来,并在textview3中显示。。。这不是我想要的

非常感谢您的帮助
~TylerNormal

您可以使用简单的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));
TextView text=findviewbyd(R.id.myTextView);
String test=“这是我的第一个数组项”;
//示例-括号中的单词将被替换并捕获
//这只是一个示例,演示如何在以后使用它进行更多操作
//复杂案件
test=test.replaceAll(“(第一个)”,“$1”);
text.setText(Html.fromHtml(test));

家庭作业??你试了什么?请添加一个代码不确定我的新编辑是否有用,但这是我的代码。请参阅下面的代码片段。它包含了你所需要的,经过适当的调整和一些思考,当然:)谢谢你,伙计,真的是我编写代码的第三天了,哈哈。但是如果我按照你的建议去做,我不是必须为每个元素输入这个代码段吗?我计划在我的两个阵列中都有数百个项目,只要你按字面意思使用它,当然你不应该这么做。你应该根据你真正需要强调的内容来调整它。列出需要突出显示的单词,将它们放入数组中,对每个输入的字符串使用replace调用在该数组中迭代,并替换该搜索数组中的字符串。replace行将转换为类似于
test=test.replaceAll(“(“+search[i]+”),“$1”)