5

我有一个TextView,其中每个单词都是一个ClickableSpan。单击时,单词变为粗体,并且单词的字典定义显示在另一个TextView中。该应用程序正常工作,直到我使TextView中的文本可选择。当文本变为可选时,定义会在单击时显示,但该单词仅在双击时才会显示为粗体。在双击或长按时选择文本(但长按不会使单词变为粗体)。

我的猜测是,问题与在动作处理过程中绘制状态更新时有关,但我无法找到解决方法。我尝试设置TextView focusable="false"但没有任何改变。相关代码如下。

curSpan = new WordSpan(index) {
    @Override
    public void onClick(View view) {
        handleWordClick(index,this); // handles code to display definition
        setMarking(true);
        view.invalidate();
        tvText.invalidate();
    }
};

spannableStringBuilder.setSpan(curSpan, totalLength, totalLength + strWord, Spanned.SPAN_COMPOSING);

和 WordSpan 定义:

class WordSpan extends ClickableSpan
    {
        int id;
        private boolean marking = false;

        public WordSpan(int id) {
            this.id = id;
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(Color.BLACK);
            ds.setUnderlineText(false);
            if (marking) {
                ds.setTypeface(Typeface.create(myFont,Typeface.BOLD));
            }
        }

        @Override
        public void onClick(View v) {}

        public void setMarking(boolean m) {
            marking = m;
        }
    }

设置 TextView 的移动方式:

private MovementMethod createMovementMethod ( Context context ) {
        final GestureDetector detector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onSingleTapUp ( MotionEvent e ) {
                    return true;
                }

                @Override
                public boolean onSingleTapConfirmed ( MotionEvent e ) {
                    return true;
                }
            });

        return new ScrollingMovementMethod() {

            @Override
            public boolean canSelectArbitrarily () {
                return true;
            }

            @Override
            public void initialize(TextView widget, Spannable text) {
                Selection.setSelection(text, text.length());
            }

            @Override
            public void onTakeFocus(TextView view, Spannable text, int dir) {
                if ((dir & (View.FOCUS_FORWARD | View.FOCUS_DOWN)) != 0) {
                    if (view.getLayout() == null) {
                        // This shouldn't be null, but do something sensible if it is.
                        Selection.setSelection(text, text.length());
                    }
                } else {
                    Selection.setSelection(text, text.length());
                }
            }

            @Override
            public boolean onTouchEvent ( TextView widget, Spannable buffer, MotionEvent event ) {
                // check if event is a single tab
                boolean isClickEvent = detector.onTouchEvent(event);

                // detect span that was clicked
                if (isClickEvent) {
                    int x = (int) event.getX();
                    int y = (int) event.getY();

                    x -= widget.getTotalPaddingLeft();
                    y -= widget.getTotalPaddingTop();

                    x += widget.getScrollX();
                    y += widget.getScrollY();

                    Layout layout = widget.getLayout();
                    int line = layout.getLineForVertical(y);
                    int off = layout.getOffsetForHorizontal(line, x);

                    WordSpan[] link = buffer.getSpans(off, off, WordSpan.class);

                    if (link.length != 0) {
                        // execute click only for first clickable span
                        // can be a for each loop to execute every one
                        if (event.getAction() == MotionEvent.ACTION_UP) {
                            link[0].onClick(widget);
                        } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
                            Selection.setSelection(buffer,
                                                   buffer.getSpanStart(link[0]),
                                                   buffer.getSpanEnd(link[0]));
                        }
                        return true;
                    }
                }

                // let scroll movement handle the touch
                return super.onTouchEvent(widget, buffer, event);
            }
        };
    }

编辑:我刚刚发现了一个可能有助于解决的新怪癖。如果我双击但在单击之间更改单词(点击一个单词然后快速点击另一个单词),第一次点击显示该单词的定义,第二次点击第一个单词是粗体但选择了第二个单词(突出显示) 并且仍然显示第一个单词的定义。

例如,如果我双击“first”然后“second”,当我点击“first”时,将显示“first”的定义,当我点击“second”时,单词“first”是粗体,单词“ second" 突出显示,但定义没有改变(仍然显示“first”的定义)。

4

1 回答 1

0

替换createMovementMethod为以下内容。如果您遇到错误,请修复它并编辑此答案。

private MovementMethod createMovementMethod (final Context context ) {

        return new ScrollingMovementMethod() {
            public MotionEvent event;
            public Spannable buffer;
            public TextView widget;
            final GestureDetector detector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
                @Override
                public boolean onSingleTapUp ( MotionEvent e ) {

                    return true;
                }

                @Override
                public boolean onSingleTapConfirmed ( MotionEvent e ) {
                    triggerClick();
                    return true;
                }

                @Override
                public boolean onDoubleTap(MotionEvent e) {
                    triggerClick();
                    return true;
                }

                private boolean triggerClick() {
                    int x = (int) event.getX();
                    int y = (int) event.getY();

                    x -= widget.getTotalPaddingLeft();
                    y -= widget.getTotalPaddingTop();

                    x += widget.getScrollX();
                    y += widget.getScrollY();

                    Layout layout = widget.getLayout();
                    int line = layout.getLineForVertical(y);
                    int off = layout.getOffsetForHorizontal(line, x);

                    WordSpan[] link = buffer.getSpans(off, off, WordSpan.class);

                    if (link.length != 0) {
                        // execute click only for first clickable span
                        // can be a for each loop to execute every one
                        if (event.getAction() == MotionEvent.ACTION_UP) {
                            link[0].onClick(widget);
                        } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
                            Selection.setSelection(buffer,
                                    buffer.getSpanStart(link[0]),
                                    buffer.getSpanEnd(link[0]));
                        }
                        return true;
                    }

                    return true;
                }
            });
            @Override
            public boolean canSelectArbitrarily () {
                return true;
            }

            @Override
            public void initialize(TextView widget, Spannable text) {
                Selection.setSelection(text, text.length());
            }

            @Override
            public void onTakeFocus(TextView view, Spannable text, int dir) {
                if ((dir & (View.FOCUS_FORWARD | View.FOCUS_DOWN)) != 0) {
                    if (view.getLayout() == null) {
                        // This shouldn't be null, but do something sensible if it is.
                        Selection.setSelection(text, text.length());
                    }
                } else {
                    Selection.setSelection(text, text.length());
                }
            }

            @Override
            public boolean onTouchEvent (TextView widget, Spannable buffer, MotionEvent event ) {
                // check if event is a single tab
                boolean isClickEvent = detector.onTouchEvent(event);

                //record this for GestureDetector
                this.widget = widget;
                this.buffer = buffer;
                this.event = event;

                // detect span that was clicked
                if (isClickEvent) {
                    //ignore click here
                    return true;
                }

                // let scroll movement handle the touch
                return super.onTouchEvent(widget, buffer, event);
            }
        };
    }
于 2016-11-28T06:49:52.527 回答