使用Adobe PDF等颜色突出显示textview

时间:2017-03-29 09:11:41

标签: android performance sharedpreferences

我尝试添加菜单以关注textview以突出显示单词或短语。 目前我的代码有问题 1.如果单词出现不止一次,则突出显示多个单词 2.应用程序退出时突出显示的颜色消失。

    TextView textView = (TextView) findViewById(R.id.textView);
    textView.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        getMenuInflater().inflate(R.menu.h_menu, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
                case R.id.highlight:
                    setTextBG();
                    return true;
                default:
                    break;
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }
    });
}
private void setTextBG() {
    String selectedText = "";
    if (textView.isFocused()) {
        final int textStartIndex = textView.getSelectionStart();
        final int textEndIndex = textView.getSelectionEnd();

        int min = 0;
        int max = textView.getText().length();

        min = Math.max(0, Math.min(textStartIndex, textEndIndex));
        max = Math.max(0, Math.max(textStartIndex, textEndIndex));
        selectedText = textView.getText().subSequence(min, max).toString().trim();
    }
    int txt = textView.getText().toString().indexOf(selectedText, 0);

    Spannable mywords = new SpannableString(textView.getText().toString());
    for (int i = 0; i < textView.getText().toString().length() && whateva != -1;
            i = whateva+1) {
        txt = textView.getText().toString().indexOf(selectedText, i);
        if (txt == -1) break;
        else {
            mywords.setSpan(new BackgroundColorSpan(Color.YELLOW), txt, txt+selectedText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(mywords, TextView.BufferType.SPANNABLE);
        }
    }
    Toast.makeText(getApplicationContext(), selectedText, Toast.LENGTH_SHORT).show();
}

1 个答案:

答案 0 :(得分:0)

请在下面的示例中突出显示内容中的多个单词。

<强> MainActivity.java

public class MainActivity extends AppCompatActivity {
    private TextView textContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textContent = (TextView) findViewById(R.id.txtContent);

        highlightWords();

    }

    private void highlightWords() {
        String content = "This is just a demo of how to highlight more than one word in Android";
        textContent.setText(getSpannableWord(content));
    }

    private Spannable getSpannableWord(String content) {
        Spannable spannableString = new SpannableString(content);

        // Create list of highlighted word and add to list every time user highlights
        ArrayList<HighlightedWord> words = new ArrayList<>();

        //1st highlighted word
        HighlightedWord highlightedWord1 = new HighlightedWord("demo", 15);
        words.add(highlightedWord1);

        //2nd highlighted word
        HighlightedWord highlightedWord2 = new HighlightedWord("highlight", 30);
        words.add(highlightedWord2);

        for (HighlightedWord highlightedWord : words) {
            spannableString.setSpan(new BackgroundColorSpan(Color.YELLOW), highlightedWord.getStartIndex(),
                    highlightedWord.getStartIndex()+ highlightedWord.getWord().length(), Spannable
                            .SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spannableString;
    }
}

谢谢!