Change the Color of Highlighted text in Android

前端 未结 5 2544
你的背包
你的背包 2021-02-20 18:54

I\'m not sure that this is possible, perhaps someone can set me straight. I have an EditText View in an android application that has white text on a blue background. When the te

5条回答
  •  难免孤独
    2021-02-20 19:30

    Object mSelectionForegroundColorSpan;
    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {
        super.onSelectionChanged(selStart, selEnd);
        if(mSelectionForegroundColorSpan == null){
            mSelectionForegroundColorSpan = new ForegroundColorSpan(Color.GREEN);
        }else{
            getText().removeSpan(mSelectionForegroundColorSpan);
        }
        if(selStart > selEnd){
            int swap = selStart;
            selStart = selEnd;
            selEnd = swap;
        }
        getText().setSpan(mSelectionForegroundColorSpan, selStart, selEnd, Spanned.SPAN_INTERMEDIATE);
    }
    

    Override the onSelectionChanged method of TextView, get the text and set a ForegroundColorSpan

提交回复
热议问题