处理粗体的方案, 需要跟MS-Word中的效果一样进行处理

以下是处理的代码, 非通用

public static class TextBoldAction {
        @Override
        public void run() {
            WordProccessView view = getCurrTextView();
            if (view != null) {
                int start = view.getSelectionStart();
                int end = view.getSelectionEnd();
                if (start == end) {
                    return;
                }
                if (start < 0 || end < 0) {
                    return;
                }
                if (start > end) {
                    int temp = start;
                    start = end;
                    end = temp;
                }

                Boolean isFirstSpanBold = false;
                Boolean isHaveBold = false;

                List<ParcelableSpan> f_list = view.getFirstSpans(
                        StyleSpan.class, start);

                if (f_list != null) {
                    Log.i(TAG, “StyleSpan.class.size()=” + f_list.size());

                    for (ParcelableSpan o : f_list) {
                        if (o instanceof StyleSpan
                                && ((StyleSpan) o).getStyle() == Typeface.BOLD) {
                            // first_Span = (StyleSpan) o;

                            isFirstSpanBold = true;
                            break;
                        }
                    }
                }

                StyleSpan[] mSpans = view.getText().getSpans(start, end,
                        StyleSpan.class);
                if (mSpans != null && mSpans.length != 0) {
                    for (int i = 0; i < mSpans.length; i++) {
                        StyleSpan span = (StyleSpan) mSpans[i];

                        if (span != null) {
                            if (span.getStyle() == Typeface.BOLD) {
                                isHaveBold = true;
                                break;
                            }
                        }
                    }
                }

                CompoundCommand command = new CompoundCommand();
                if (isHaveBold) {
                    int _start;
                    int _end;
                    if (isFirstSpanBold) {// 第一个字为粗体

                        for (int i = 0; i < mSpans.length; i++) {
                            StyleSpan span = (StyleSpan) mSpans[i];

                            if (span != null) {
                                if (span.getStyle() == Typeface.BOLD) {

                                    _start = view.getText().getSpanStart(span);
                                    _end = view.getText().getSpanEnd(span);

                                    Log.i(TAG, “1.删除span”);
                                    RemoveSpanCommand mRemoveSpanCommand = new RemoveSpanCommand(
                                            view, span, _start, _end);
                                    command.append(mRemoveSpanCommand);

                                    if (_start < start) {
                                        // 如果第一个是粗体的话
                                        StyleSpan b1 = new StyleSpan(
                                                Typeface.BOLD);
                                        Log.i(TAG, “2.添加_start->start”);
                                        ChangeSpanCommand changeSpanCommand = new ChangeSpanCommand(
                                                view, b1, _start, start);
                                        command.append(changeSpanCommand);
                                        if (_end > end) {

                                            Log.i(TAG, “3.添加end->_end”);
                                            StyleSpan b2 = new StyleSpan(
                                                    Typeface.BOLD);
                                            changeSpanCommand = new ChangeSpanCommand(
                                                    view, b2, end, _end);
                                            command.append(changeSpanCommand);
                                        }

                                    } else if (_start >= start) {
                                        if (_end > end) {
                                            Log.i(TAG, “4.添加end->_end”);
                                            ChangeSpanCommand changeSpanCommand = new ChangeSpanCommand(
                                                    view, span, end, _end);
                                            command.append(changeSpanCommand);
                                        }
                                    }

                                }
                            }
                        }
                    } else { // 第一个字不为粗体

                        for (int i = 0; i < mSpans.length; i++) {
                            StyleSpan span = (StyleSpan) mSpans[i];

                            if (span != null) {
                                if (span.getStyle() == Typeface.BOLD) {

                                    _start = view.getText().getSpanStart(span);
                                    _end = view.getText().getSpanEnd(span);

                                    Log.i(TAG, “1.删除span”);
                                    RemoveSpanCommand mRemoveSpanCommand = new RemoveSpanCommand(
                                            view, span, _start, _end);
                                    command.append(mRemoveSpanCommand);

                                    // 添加span
                                    StyleSpan span_bold = new StyleSpan(
                                            Typeface.BOLD);
                                    ChangeSpanCommand mChangeSpanCommand = new ChangeSpanCommand(
                                            view, span_bold, start, end);
                                    command.append(mChangeSpanCommand);
                                }
                            }
                        }
                    }

                } else {

                    StyleSpan span = new StyleSpan(Typeface.BOLD);
                    ChangeSpanCommand mChangeSpanCommand = new ChangeSpanCommand(
                            view, span, start, end);
                    command.append(mChangeSpanCommand);
                }
                ExecutionUtil.execute(command);
                TextCursorManager.setSelections(start, end, view);
                TextCursorManager.show();

                // WordManager.Instance().TextParams_B.setValue(true);
                List<ParcelableSpan> list = view.getSpans(start, end);
                IRootView iRootView = XNoteGlobal.getIRootView();
                iRootView.changeTextPanel(list);
                iRootView = null;

            }
        }
    }

ps: Android字体处理