Change the Color of Highlighted text in Android

前端 未结 5 4988
栀梦
栀梦 2021-02-20 19:13

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:18

    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

提交回复
热议问题