Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.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
Java 将颜色设置为可扩展文本的多个非连续部分_Java_Android_Textview_Spannablestring - Fatal编程技术网

Java 将颜色设置为可扩展文本的多个非连续部分

Java 将颜色设置为可扩展文本的多个非连续部分,java,android,textview,spannablestring,Java,Android,Textview,Spannablestring,我有一个可扩展的文本。它有一个字符串表示例如,TO,BE,IS,DO。我正在从数据库[BE,DO]读取arraylist。我正试图改变生活的颜色。 我的java代码: Spannable spans; private void clickableDictionary(String words) { final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(33, 150, 243)); ArrayLis

我有一个可扩展的文本。它有一个字符串表示例如,TO,BE,IS,DO。我正在从数据库[BE,DO]读取arraylist。我正试图改变生活的颜色。 我的java代码:

Spannable spans;
private void clickableDictionary(String words) {

    final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.rgb(33, 150, 243));

    ArrayList<String> myList = new ArrayList<>(Arrays.asList(words.split(", ")));
    String vocab = object.fetchDataVOCAB(getApplicationContext(), wordLength);
    ArrayList<String> myListVOCAB = new ArrayList<>(Arrays.asList(vocab.split(", ")));

    words_done.setMovementMethod(LinkMovementMethod.getInstance()); //words_done is the textView
    words_done.setText(words, TextView.BufferType.SPANNABLE);
    spans = (Spannable) words_done.getText();
    if (myListVOCAB.toString().replace("[", "").replace("]", "") != "") {
        System.out.println("-------VOCAB: "+myListVOCAB+" size of done words: "+myList.size());
        int indexStart;
        int indexEnd;

        for (int i = 0; i < myListVOCAB.size(); i++) {

            indexStart = words.indexOf(myListVOCAB.get(i));
            System.out.println("-------VOCAB startIndex: "+indexStart+"for loop index:---"+i);
            indexEnd = indexStart + (myListVOCAB.get(i).length());
            System.out.println("-------VOCAB endIndex: " + indexEnd);

            spans.setSpan(fcs, indexStart, indexEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            System.out.println("-------color change startIndex: " + indexStart + "endIndex:---" + indexEnd);
        }
    }

        BreakIterator iterator = BreakIterator.getWordInstance(Locale.US);
        iterator.setText(words);
        int start = iterator.first();
        for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator
                .next()) {
            String possibleWord = words.substring(start, end);
            if (Character.isLetterOrDigit(possibleWord.charAt(0))) {
                ClickableSpan clickSpan = getClickableSpan(possibleWord, start, end);

                spans.setSpan(clickSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            }
        }

}

private ClickableSpan getClickableSpan(final String word, final int start, final int end) {
    return new ClickableSpan() {
        final String mWord;
        {
            mWord = word;
        }

        @Override
        public void onClick(View widget) {

            String s = object.fetchDataVOCAB(getApplicationContext(), wordLength);

            if (s.isEmpty()) {
                s = mWord;
                object.writeVOCAB(getApplicationContext(), wordLength, s, vocabCount + 1);
                vocab.setText("Vocab: " + object.readVOCAB(getApplicationContext(), wordLength));
            }

            else {
                if (!s.contains(mWord)) {
                    s = s + ", " + mWord;
                    object.writeVOCAB(getApplicationContext(), wordLength, s, object.readVOCAB(getApplicationContext(), wordLength) + 1);
                    vocab.setText("Vocab: " + object.readVOCAB(getApplicationContext(), wordLength));
                }
            }

            spans.setSpan(new ForegroundColorSpan(Color.rgb(33, 150, 243)), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            new atask().execute(mWord);
            widget.invalidate();
        }

        public void updateDrawState(TextPaint ds) {// override updateDrawState
            ds.setUnderlineText(false); // set to false to remove underline
        }

    };

}
span可跨越的跨度;
专用void clickableDictionary(字符串词){
最终ForegroundColorSpan fcs=新的ForegroundColorSpan(Color.rgb(33150243));
ArrayList myList=新的ArrayList(Arrays.asList(words.split(“,”));
字符串vocab=object.fetchDataVOCAB(getApplicationContext(),字长);
ArrayList myListVOCAB=新的ArrayList(Arrays.asList(vocab.split(“,”));
words_done.setMovementMethod(LinkMovementMethod.getInstance());//words_done是文本视图
words_done.setText(words,TextView.BufferType.SPANNABLE);
span=(Spannable)words_done.getText();
如果(myListVOCAB.toString().replace(“[”,”).replace(“]”,“)!=”){
System.out.println(“----VOCAB:+myListVOCAB+”完成单词的大小:+myList.size());
int indexStart;
int指数;
对于(int i=0;i
我的列表包含我想打印为可点击的单词。myListVOCAB包含我想用蓝色打印的单词。我正在从数据库中填充这些。同时,我也试图在点击的时候让一个单词变成蓝色。
我的问题是,当意图开始时,只有一个单词以蓝色打印,这是myListVOCAB的最后一个单词。我想我没有相应地使用setSpan方法。请分享一些关于如何设置Spanable的部分以更改颜色的建议,这些部分不是字符串中预先设置的。

更改列表中字符串颜色的方法如下:

TextView TV = (TextView)findViewById(R.id.mytextview01);

Spannable wordtoSpan = new SpannableString(list.get(0));  



wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

TV.setText(wordtoSpan);

现在您只需要管理列表中的文本。

请您再解释一下好吗?我想我不太明白你的答案。我需要在同一文本中打印蓝色单词和非蓝色单词。蓝色文字也必须是可点击的@Rajan BhavsarIn第三个语句我们必须为文本的第一个位置和最后一个位置设置索引,我们需要将颜色设置为蓝色。是的,我明白了。我想说的是,我怎样才能在同一个页面上打印“BE(蓝色)、is(黑色)、DO(蓝色)、NO(黑色)、ON(黑色)”,这样所有内容都可以点击@Rajan BhavsarAre你有预定义的字符串,比如如果字符串是BE,那么我们比较字符串并用蓝色打印该字符串,等等。当我这样做时,只有最后一个字符串变为蓝色。请检查下面的字符串,我想要:“BE(蓝色),IS(黑色),DO(蓝色),NO(黑色),ON(黑色)”我得到:“BE(黑色,我想要的黑色),IS(黑色),DO(蓝色),NO(黑色),ON(黑色)”