gpt4 book ai didi

android - 添加 MotionEvent 后 WebView TextSelection 消失了

转载 作者:行者123 更新时间:2023-11-30 00:55:17 25 4
gpt4 key购买 nike

大家好我正在创建一个 epubreader 并在 webview 中加载这本书,我让 webview 水平滚动添加一些 css 规则,现在我添加了左右滑动以根据屏幕宽度翻页,我得到了屏幕检测使用此类的左右滑动功能

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;

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

public void onSwipeLeft() {
}

public void onSwipeRight() {
}

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

private final class GestureListener extends SimpleOnGestureListener {

private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;

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

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}

}
}

在我的主 Activity 中是这样实现的

   webView.setOnTouchListener(new OnSwipeTouchListener(this) {
@Override
public void onSwipeLeft() {
// Whatever
if(TotalPages>PresentPage){
PresentPage++;
forward.start();
webView.scrollTo(PresentPage*ScreenWidth, 0);

}
else{
Toast.makeText(getApplicationContext(), "The End", 100).show();
}
}
@Override
public void onSwipeRight() {

if(PresentPage>0)
{
PresentPage--;
back.start();
webView.scrollTo(PresentPage*ScreenWidth, 0);
}
}
});

现在左右手势工作正常但默认文本选择功能(即长按选择文本)消失了,我想要带有滑动检测的默认文本选择,我怎样才能得到它?提前致谢

最佳答案

用这个类解决

public class OnSwipeTouchListener implements OnTouchListener {

private final GestureDetector gestureDetector;
private static final int MAX_CLICK_DURATION = 1000;
private static final int MAX_CLICK_DISTANCE = 15;
static Context mcontext;
private long pressStartTime;
private float pressedX;
private float pressedY;
private boolean stayedWithinClickDistance;

public OnSwipeTouchListener(Context context) {
gestureDetector = new GestureDetector(context, new GestureListener());
this.mcontext=context;
}

public void onSwipeLeft() {
}

public void onSwipeRight() {
}
public void newTouch(){

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

if(event.getAction()== MotionEvent.ACTION_MOVE)
{
if (stayedWithinClickDistance && distance(pressedX, pressedY, event.getX(), event.getY()) > MAX_CLICK_DISTANCE) {
stayedWithinClickDistance = false;
}
return true;
}

else if (event.getAction()== MotionEvent.ACTION_DOWN) {
pressStartTime = System.currentTimeMillis();
pressedX = event.getX();
pressedY = event.getY();
stayedWithinClickDistance = true;

return v.onTouchEvent(event);
}
else if(event.getAction()== MotionEvent.ACTION_UP) {

long pressDuration = System.currentTimeMillis() - pressStartTime;
if (pressDuration < MAX_CLICK_DURATION && stayedWithinClickDistance) {
newTouch();
}

return v.onTouchEvent(event);
}
else{
return v.onTouchEvent(event);
}

}
private static float distance(float x1, float y1, float x2, float y2) {
float dx = x1 - x2;
float dy = y1 - y2;
float distanceInPx = (float) Math.sqrt(dx * dx + dy * dy);
return pxToDp(distanceInPx);
}

private static float pxToDp(float px) {
return px / mcontext.getResources().getDisplayMetrics().density;
}


private final class GestureListener extends SimpleOnGestureListener {

private static final int SWIPE_DISTANCE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;

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

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (distanceX > 0)
onSwipeRight();
else
onSwipeLeft();
return true;
}
return false;
}

}


}

在主要 Activity 中

 webView.setOnTouchListener(new OnSwipeTouchListener(this) {
@Override
public void onSwipeLeft() {
// Whatever
// for swiping left action
}

@Override
public void onSwipeRight() {
// TODO Auto-generated method stub

//for swiping right action

}
@Override
public void newTouch() {

// for single tap action

}

});

在此之后你仍然会有默认的文本选择选项

关于android - 添加 MotionEvent 后 WebView TextSelection 消失了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40278390/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com