编辑跨区文本

时间:2016-03-29 17:15:43

标签: android spanned

我需要为我的项目设计一个编辑器,允许用户设置文本的某些部分。 我通过在按钮的EditText中添加以下代码,使用了onClickListener和粗体选定的文字。

String selectedText = "<b>" + mEditTextContent.getText().toString().substring(
                    mEditTextContent.getSelectionStart(),
                    mEditTextContent.getSelectionEnd()
            ) + "</b>";

mEditTextContent.getText().replace(
                    mEditTextContent.getSelectionStart(),
                    mEditTextContent.getSelectionEnd(),
                    Html.fromHtml(selectedText)
            );

但我很想知道如何切换大胆的风格?例如,如果选定的文本已经加粗,则将其展开,如果没有,则加粗。

1 个答案:

答案 0 :(得分:1)

我写这些代码以切换粗体和&amp;所选文本的斜体样式。

创建一个类并将其命名为public class SpanStyleHelper { protected EditText mEditText; protected Spannable mSpannable; protected int mSelectedTextStart; protected int mSelectedTextEnd; public SpanStyleHelper(EditText editText) { mEditText = editText; mSpannable = mEditText.getText(); mSelectedTextStart = mEditText.getSelectionStart(); mSelectedTextEnd = mEditText.getSelectionEnd(); } public Spannable boldSelectedText() { Log.d("Ramansoft", "Try to bold selected text.."); StyleSpan[] styleSpans = mEditText.getText().getSpans( mSelectedTextStart, mSelectedTextEnd, StyleSpan.class ); if(styleSpans.length > 0) { int lastSpanEnd = 0; for (StyleSpan styleSpan : styleSpans) { /** * Save old style */ int oldStyle = styleSpan.getStyle(); /** * Get start and end of span */ int spanStart = mSpannable.getSpanStart(styleSpan); int spanEnd = mSpannable.getSpanEnd(styleSpan); /** * Before bold this span, we check if any unspanned * text between this span and last span remains. if any * unspanned text exist, we should bold it */ if(spanStart > lastSpanEnd) { mSpannable.setSpan( new StyleSpan(Typeface.BOLD), lastSpanEnd, spanStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } /** * Update last span end */ lastSpanEnd = spanEnd; /** * Remove the span */ mSpannable.removeSpan(styleSpan); /** * Because we just need change selected text, * if span start is lower than selected text start or * if span end is higher than selected text end start * we should restore span for unselected part of span */ if (spanStart < mEditText.getSelectionStart()) { mSpannable.setSpan( new StyleSpan(oldStyle), spanStart, mSelectedTextStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } if (spanEnd > mEditText.getSelectionEnd()) { mSpannable.setSpan( new StyleSpan(oldStyle), mSelectedTextEnd, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } /** * We want to add bold style to current style * so we most detect current style and change * the style depend on current style */ if (oldStyle == Typeface.ITALIC) { mSpannable.setSpan( new StyleSpan(Typeface.BOLD_ITALIC), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } else { mSpannable.setSpan( new StyleSpan(Typeface.BOLD), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } } /** * Now we should check if any * unspanned selected text remains */ if(mSelectedTextEnd != lastSpanEnd) { mSpannable.setSpan( new StyleSpan(Typeface.BOLD), lastSpanEnd, mSelectedTextEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } } else { mSpannable.setSpan( new StyleSpan(Typeface.BOLD), mSelectedTextStart, mSelectedTextEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } return mSpannable; } public Spannable unBoldSelectedText() { Log.d("Ramansoft", "Try to unbold selected text.."); StyleSpan[] styleSpans = mEditText.getText().getSpans( mSelectedTextStart, mSelectedTextEnd, StyleSpan.class ); for(StyleSpan styleSpan:styleSpans) { /** * Save old style */ int oldStyle = styleSpan.getStyle(); /** * Get start and end of span */ int spanStart = mSpannable.getSpanStart(styleSpan); int spanEnd = mSpannable.getSpanEnd(styleSpan); /** * Remove the span */ mSpannable.removeSpan(styleSpan); /** * Because we just need change selected text, * if span start is lower than selected text start or * if span end is higher than selected text end start * we should restore span for unselected part of span */ if(spanStart < mEditText.getSelectionStart()) { mSpannable.setSpan( new StyleSpan(oldStyle), spanStart, mSelectedTextStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } if(spanEnd > mEditText.getSelectionEnd()) { mSpannable.setSpan( new StyleSpan(oldStyle), mSelectedTextEnd, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } /** * Because we just want to remove bold style, * if the span has another style, we should restore it */ if(oldStyle == Typeface.BOLD_ITALIC) { mSpannable.setSpan( new StyleSpan(Typeface.ITALIC), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } } return mSpannable; } public Spannable toggleBoldSelectedText() { Log.d("Ramansoft", "Try to toggle bold selected text.."); boolean isAllSpansBold = true; StyleSpan[] styleSpans = mEditText.getText().getSpans( mSelectedTextStart, mSelectedTextEnd, StyleSpan.class ); if(styleSpans.length == 0) { return boldSelectedText(); } else { for(StyleSpan styleSpan:styleSpans) { Log.d("Ramansoft", "styleSpan.getStyle() = " + styleSpan.getStyle()); if (styleSpan.getStyle() != Typeface.BOLD && styleSpan.getStyle() != Typeface.BOLD_ITALIC) { isAllSpansBold = false; break; } } Log.d("Ramansoft", "isAllSpansBold = " + isAllSpansBold); if(isAllSpansBold) return unBoldSelectedText(); else return boldSelectedText(); } } public Spannable italicSelectedText() { Log.d("Ramansoft", "Try to italic selected text.."); StyleSpan[] styleSpans = mEditText.getText().getSpans( mSelectedTextStart, mSelectedTextEnd, StyleSpan.class ); if(styleSpans.length > 0) { int lastSpanEnd = 0; for (StyleSpan styleSpan : styleSpans) { /** * Save old style */ int oldStyle = styleSpan.getStyle(); /** * Get start and end of span */ int spanStart = mSpannable.getSpanStart(styleSpan); int spanEnd = mSpannable.getSpanEnd(styleSpan); /** * Before italic this span, we check if any unspanned * text between this span and last span remains. if any * unspanned text exist, we should italic it */ if(spanStart > lastSpanEnd) { mSpannable.setSpan( new StyleSpan(Typeface.ITALIC), lastSpanEnd, spanStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } /** * Update last span end */ lastSpanEnd = spanEnd; /** * Remove the span */ mSpannable.removeSpan(styleSpan); /** * Because we just need change selected text, * if span start is lower than selected text start or * if span end is higher than selected text end start * we should restore span for unselected part of span */ if (spanStart < mEditText.getSelectionStart()) { mSpannable.setSpan( new StyleSpan(oldStyle), spanStart, mSelectedTextStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } if (spanEnd > mEditText.getSelectionEnd()) { mSpannable.setSpan( new StyleSpan(oldStyle), mSelectedTextEnd, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } /** * We want to add bold style to current style * so we most detect current style and change * the style depend on current style */ if (oldStyle == Typeface.BOLD) { mSpannable.setSpan( new StyleSpan(Typeface.BOLD_ITALIC), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } else { mSpannable.setSpan( new StyleSpan(Typeface.ITALIC), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } } /** * Now we should check if any * unspanned selected text remains */ if(mSelectedTextEnd != lastSpanEnd) { mSpannable.setSpan( new StyleSpan(Typeface.ITALIC), lastSpanEnd, mSelectedTextEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } } else { mSpannable.setSpan( new StyleSpan(Typeface.ITALIC), mSelectedTextStart, mSelectedTextEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } return mSpannable; } public Spannable unItalicSelectedText() { Log.d("Ramansoft", "Try to un-italic selected text.."); StyleSpan[] styleSpans = mEditText.getText().getSpans( mSelectedTextStart, mSelectedTextEnd, StyleSpan.class ); for(StyleSpan styleSpan:styleSpans) { /** * Save old style */ int oldStyle = styleSpan.getStyle(); /** * Get start and end of span */ int spanStart = mSpannable.getSpanStart(styleSpan); int spanEnd = mSpannable.getSpanEnd(styleSpan); /** * Remove the span */ mSpannable.removeSpan(styleSpan); /** * Because we just need change selected text, * if span start is lower than selected text start or * if span end is higher than selected text end start * we should restore span for unselected part of span */ if(spanStart < mEditText.getSelectionStart()) { mSpannable.setSpan( new StyleSpan(oldStyle), spanStart, mSelectedTextStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } if(spanEnd > mEditText.getSelectionEnd()) { mSpannable.setSpan( new StyleSpan(oldStyle), mSelectedTextEnd, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } /** * Because we just want to remove bold style, * if the span has another style, we should restore it */ if(oldStyle == Typeface.BOLD_ITALIC) { mSpannable.setSpan( new StyleSpan(Typeface.BOLD), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ); } } return mSpannable; } public Spannable toggleItalicSelectedText() { Log.d("Ramansoft", "Try to toggle italic selected text.."); boolean isAllSpansItalic = true; StyleSpan[] styleSpans = mEditText.getText().getSpans( mSelectedTextStart, mSelectedTextEnd, StyleSpan.class ); if(styleSpans.length == 0) { return italicSelectedText(); } else { for(StyleSpan styleSpan:styleSpans) { Log.d("Ramansoft", "styleSpan.getStyle() = " + styleSpan.getStyle()); if (styleSpan.getStyle() != Typeface.ITALIC && styleSpan.getStyle() != Typeface.BOLD_ITALIC) { isAllSpansItalic = false; break; } } Log.d("Ramansoft", "isAllSpansItalic = " + isAllSpansItalic); if(isAllSpansItalic) return unItalicSelectedText(); else return italicSelectedText(); } }

SpanStyleHelper

以下是OnClickListener中使用public class onButtonBoldClick implements ImageButton.OnClickListener { @Override public void onClick(View v) { SpanStyleHelper spanStyleHelper = new SpanStyleHelper(mEditTextContent); mEditTextContent.setText( spanStyleHelper.toggleBoldSelectedText() ); } } public class onButtonItalicClick implements ImageButton.OnClickListener { @Override public void onClick(View v) { SpanStyleHelper spanStyleHelper = new SpanStyleHelper(mEditTextContent); mEditTextContent.setText( spanStyleHelper.toggleItalicSelectedText() ); } } 的代码:

<section id="google-map" class="gmap slider-parallax"></section>

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="js/jquery.gmap.js"></script>

<script type="text/javascript">
    $('#google-map').gMap({

        address: 'riyadh, saudi arabia',
        maptype: 'ROADMAP',
        zoom: 6,
        markers: [{
            address: "riyadh, saudi arabia",
            html: '<div style="width: 300px;"><h4 style="margin-bottom: 8px;">Hi, we\'re <span>Envato</span></h4><p class="nobottommargin">Our mission is to help people to <strong>earn</strong> and to <strong>learn</strong> online. We operate <strong>marketplaces</strong> where hundreds of thousands of people buy and sell digital goods every day, and a network of educational blogs where millions learn <strong>creative skills</strong>.</p></div>',
            icon: { image: "images/icons/map-icon-red.png", iconsize: [32, 39], iconanchor: [13, 39] }
        },
    {
        address: "Al Uyaynah العيينة",
        html: '<div style="width: 300px;"><h4 style="margin-bottom: 8px;">Hi, we\'re <span>Envato</span></h4><p class="nobottommargin">Ring Road, Al Abageyah, Qism El-KhalifaOur mission is to help people to <strong>earn</strong> and to <strong>learn</strong> online. We operate <strong>marketplaces</strong> where hundreds of thousands of people buy and sell digital goods every day, and a network of educational blogs where millions learn <strong>creative skills</strong>.</p></div>',
        icon: { image: "images/icons/map-icon-red.png", iconsize: [32, 39], iconanchor: [13, 39] }
    },
             {
                 address: "Riyadh Province, Saudi Arabia",
                 html: '<div style="width: 300px;"><h4 style="margin-bottom: 8px;">Hi, we\'re <span>Envato</span></h4><p class="nobottommargin">Ring Road, Al Abageyah, Qism El-KhalifaOur mission is to help people to <strong>earn</strong> and to <strong>learn</strong> online. We operate <strong>marketplaces</strong> where hundreds of thousands of people buy and sell digital goods every day, and a network of educational blogs where millions learn <strong>creative skills</strong>.</p></div>',
                 icon: { image: "images/icons/map-icon-red.png", iconsize: [32, 39], iconanchor: [13, 39] }
             }]
            ,

        doubleclickzoom: false,
        controls: {
            panControl: true,
            zoomControl: true,
            mapTypeControl: true,
            scaleControl: false,
            streetViewControl: false,
            overviewMapControl: false
        }
    });
</script>
相关问题