在Android TextView

时间:2015-11-20 07:18:58

标签: android textview

我在可选择的Android TextView中取消选择文本时遇到了崩溃。当我选择TextView并设置LinkMovementMethod时会发生这种情况。

IllegalArgumentException in TextView

似乎这是Android内部的一个错误。

java.lang.IllegalArgumentException: Invalid offset: -1. Valid range is [0, 10562]
    at android.text.method.WordIterator.checkOffsetIsValid(WordIterator.java:380)
    at android.text.method.WordIterator.isBoundary(WordIterator.java:101)
    at android.widget.Editor$SelectionStartHandleView.positionAtCursorOffset(Editor.java:4260)
    at android.widget.Editor$HandleView.updatePosition(Editor.java:3708)
    at android.widget.Editor$PositionListener.onPreDraw(Editor.java:2507)
    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2055)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
    at android.view.Choreographer.doCallbacks(Choreographer.java:670)
    at android.view.Choreographer.doFrame(Choreographer.java:606)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

3 个答案:

答案 0 :(得分:14)

感谢mariotaku的答案。出于某种原因,我不会在TextView中选择文本。我稍微调整了一下,以便在选择某些字符时触发工作。现在似乎工作得很好!

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    // FIXME simple workaround to https://code.google.com/p/android/issues/detail?id=191430
    int startSelection = getSelectionStart();
    int endSelection = getSelectionEnd();
    if (startSelection != endSelection) {
        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
            final CharSequence text = getText();
            setText(null);
            setText(text);
        }
    }
    return super.dispatchTouchEvent(event);
}

答案 1 :(得分:3)

我发现Android在调用setText时会清除TextView选项,所以这里有一个解决这个问题的简单方法。

public class FixedTextView extends TextView {

    public FixedTextView(final Context context) {
        super(context);
    }

    public FixedTextView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    public FixedTextView(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        // FIXME simple workaround to https://code.google.com/p/android/issues/detail?id=191430
        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
            final CharSequence text = getText();
            setText(null);
            setText(text);
        }
        return super.dispatchTouchEvent(event);
    }

}

答案 2 :(得分:1)

这是一个简单完美的解决方法。你应该把它放到你的Activity类中:

@Override
public void onActionModeStarted(ActionMode mode) {
    super.onActionModeStarted(mode);
    yourTextView.setMovementMethod(ArrowKeyMovementMethod.getInstance());
}

@Override
public void onActionModeFinished(ActionMode mode) {
    super.onActionModeFinished(mode);
    yourTextView.setMovementMethod(LinkMovementMethod.getInstance());
}