无法使用ontouchListener选择TextView

时间:2019-07-06 13:11:17

标签: java android textview ontouchlistener

我在活动中实现了ontouchListener,以检测向左和向右滑动。但是,启用此功能使我无法选择文本,因此无法获取默认的Android文本选择光标,菜单。

经过多次试验,现在我可以在长按textView时完美地调用onLongClick()方法。但是,文本仍然是不可选择的。而且,每当我禁用滑动检测时,“文本”选择都可以正常工作。

Class constructor Client cannot be invoked without 'new'

OnSwipeTouchListener类如下:

public class PreviewActivity extends AppCompatActivity
{

TextView question;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_preview);
    question = (TextView)findViewById(R.id.question);
    question.setTextSize(TextViewSize);
    question.setTextIsSelectable(true);
    question.setLongClickable(true);
    question.setFocusableInTouchMode(true);
    View prev_act = (View) findViewById(R.id.question);
    prev_act.setOnTouchListener(new OnSwipeTouchListener(this) {
        @Override public void onSwipeLeft() {
            if(RowIndex>0){
                Qtitle = ListItems.get(RowIndex-1);
                Query();
                //text = Query();

            //question.setText(text);
            questionSC.scrollTo(0,0);
            RowIndex--;
             }
        }
        @Override public void onSwipeRight() {
            if(RowIndex<ListItems.size()-1){
                Qtitle = ListItems.get(RowIndex+1);
                Query();
                //text = Query();

                //question.setText(text);
                questionSC.scrollTo(0,0);
                RowIndex++;
            }
        }
        @Override public void onLongClick() {
            Log.v(this.toString(), "Long click.");
            question.setCursorVisible(true);
            question.performLongClick();
        }
    });
}

}

我希望能够从TextView中选择一个部分(文本)并带有滑动检测器。

已编辑:添加了上述活动的XML文件

public class OnSwipeTouchListener implements OnTouchListener {
    private final GestureDetector gestureDetector;

public OnSwipeTouchListener (Context ctx){
    gestureDetector = new GestureDetector(ctx, new GestureListener());
}

@Override public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 250;
private static final int SWIPE_VELOCITY_THRESHOLD = 200;

@Override
public void onLongPress(MotionEvent e) {
    onLongClick();
    super.onLongPress(e);
}

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

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    boolean result = false;
    try {
        float diffY = e2.getY() - e1.getY();
        float diffX = e2.getX() - e1.getX();
        if (Math.abs(diffX) > Math.abs(diffY)) {
            if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                if (diffX > 0) {
                    onSwipeRight();
                } else {
                    onSwipeLeft();
                }
                result = true;
            }
        } 
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}
}

    public void onSwipeRight() {
    }
   public void onSwipeLeft() {
   }
   public void onSwipeTop() {
   }
   public void onSwipeBottom() {
   }

public void onLongClick() {
}

2 个答案:

答案 0 :(得分:0)

尝试了一段时间后,突出显示并选择文本以启用复制,粘贴,全选...等功能

您将需要在prev_act.setOnTouchListener(new OnSwipeTouchListener(this) {

中添加onTouch片段。
@Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                //do stuff here
                Log.i("clicking", "onActionDown");
            }else if(event.getAction() == MotionEvent.ACTION_UP) {

                question.performLongClick();
            }

            return false;
        }
        @Override public void onLongClick() {
        }

答案 1 :(得分:0)

答案很简单:

@Override
public boolean onDown(MotionEvent e) {
    return false;
}

我必须将onDown()设置为返回false。