OnTouchEvent中的精心选择文本

时间:2012-02-05 11:01:16

标签: android ontouchevent textselection

我编写了一个代码来选择文本的一部分但是我找不到开头和结尾的确切点。

我的代码:

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final int action = event.getAction();

        switch (action) {
        case MotionEvent.ACTION_DOWN:
        {
            lastX = (int) event.getX();
            lastY = (int) event.getY();
            break;
        }
        case MotionEvent.ACTION_UP:
        {
                int currentX = (int) event.getX();
                int currentY = (int) event.getY();
                int xDistance = Math.abs(currentX - lastX);
                int yDistance = Math.abs(currentY - lastY);

            final Spannable WordtoSpan = (Spannable) mText;

                int line = mLayout.getLineForVertical(yDistance);
                int offset = mLayout.getOffsetForHorizontal(line, xDistance);
                WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), line,
offset, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                txtContent.setText(WordtoSpan);

                break;
        }
        }
    }

请帮助我,我真的需要它。

0 个答案:

没有答案