自定义TextView无法显示标准复制粘贴

时间:2014-10-29 00:42:40

标签: android textview

我正在使用ScaleGestureDetector和GestureDetector创建自定义TextView。我的目的是在DoubleTap事件或OnScale事件上重新调整文本大小。问题是当我做LongTap时,我无法在Android上显示标准功能Copy Paste。当我使用普通的TextView时没有出现这个问题,但我不能使用DoubleTap,因为它会触发Android上的标准复制粘贴。

这是我的代码

package com.text.apps.ux;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.TextView;

public class RTextView extends TextView{

    private static final String TAG = "InteractiveTextView";

    /**
     * The number of individual points (samples) in the chart series to draw onscreen.
     */
    private static final float DEFAULT_FONT_SIZE = 7.0f;


    private float mTextSize;
    private Context mContext;
    private Paint mTextPaint;


    private ScaleGestureDetector mScaleGestureDetector;
    private GestureDetectorCompat mGestureDetector;

    public RTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initiliaze();
    }

    private void initiliaze() {
        mTextPaint = new Paint();
        mTextPaint.set(this.getPaint());

        this.setTextSize(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_FONT_SIZE);



        mTextSize = this.getTextSize();

        this.setTextIsSelectable(true);



     // Sets up interactions
        mScaleGestureDetector = new ScaleGestureDetector(mContext, mScaleGestureListener);
        mGestureDetector = new GestureDetectorCompat(mContext, mGestureListener);


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean retVal = mScaleGestureDetector.onTouchEvent(event);
        retVal = mGestureDetector.onTouchEvent(event) || retVal;

        return retVal || super.onTouchEvent(event);
    }

    public RTextView(Context context) {
        super(context);
        mContext = context;
        initiliaze();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(mTextSize<0.0f) mTextSize = DEFAULT_FONT_SIZE;

        this.setTextSize (TypedValue.COMPLEX_UNIT_DIP, mTextSize);
    }

    /**
     * The scale listener, used for handling multi-finger scale gestures.
     */
    private final ScaleGestureDetector.OnScaleGestureListener mScaleGestureListener
    = new ScaleGestureDetector.SimpleOnScaleGestureListener() {
        /**
         * This is the active focal point in terms of the viewport. Could be a local
         * variable but kept here to minimize per-frame allocations.
         */

        @Override
        public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {

            return true;
        }

        @Override
        public boolean onScale(ScaleGestureDetector scaleGestureDetector) {

            float scaleFactor = scaleGestureDetector.getScaleFactor();
            if(scaleFactor<1) {
                mTextSize -=1;
            }else{
                mTextSize ++;
            }

            invalidate();
            return true;
        }
    };


    private void selectText(){
        this.setTextIsSelectable(true);
        this.setFocusableInTouchMode(true);
    }

    /**
     * The gesture listener, used for handling simple gestures such as double touches, scrolls,
     * and flings.
     */
    private final GestureDetector.SimpleOnGestureListener mGestureListener
            = new GestureDetector.SimpleOnGestureListener() {

        @Override
        public void onShowPress(MotionEvent e){

            selectText();   
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            mTextSize ++;
            invalidate();
            return true;
        }


    };
}

提前致谢

0 个答案:

没有答案