如何设置突出显示,下划线和删除线编辑文本的if条件?

时间:2018-11-01 03:15:48

标签: java android android-studio

我已成功在应用程序中加粗和取消加粗了编辑文本,但是我被困在该如何检查突出显示颜色,删除线和下划线文本的条件?以下是我用于粗体和非粗体文本的代码。如何设置条件“如果(我的文本突出显示)”,然后删除下划线,删除线和突出显示的颜色?

 int selectionStart = bodyText.getSelectionStart();
    int selectionEnd = bodyText.getSelectionEnd();
    if (selectionStart > selectionEnd) {
        int temp = selectionEnd;
        selectionEnd = selectionStart;
        selectionStart = temp;
    }
    if (selectionEnd > selectionStart) {
        Spannable str = bodyText.getText();
        boolean exists = false;
        StyleSpan[] styleSpans;
        styleSpans = str.getSpans(selectionStart, selectionEnd, StyleSpan.class);
        // If the selected text-part already has BOLD style on it, then
        // we need to disable it
        for (int i = 0; i < styleSpans.length; i++) {
            if (styleSpans[i].getStyle() == android.graphics.Typeface.BOLD) {
                str.removeSpan(styleSpans[i]);
                exists = true;
            }
        }
        // Else we set BOLD style on it
        if (!exists) {
            str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), selectionStart, selectionEnd,
                    Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        }

        bodyText.setSelection(selectionStart, selectionEnd);
    }

0 个答案:

没有答案