gpt4 book ai didi

android - 方法 onTouchEvent 未被调用

转载 作者:IT老高 更新时间:2023-10-28 21:57:40 28 4
gpt4 key购买 nike

我的方法有问题

@Override
public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}

永远不会被调用。任何想法为什么会这样?我正在构建一个谷歌的 API 4.0.3 应用程序,并且我正在尝试为我的 ViewFliper 启用滑动。但是,它无法工作,因为从不调用 touch。

代码:

 public class MainActivity extends SherlockMapActivity implements ActionBar.TabListener {

这就是我的 Activity 声明。并检测我已经实现的滑动:

    SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener(){

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {

float sensitvity = 50;
if((e1.getX() - e2.getX()) > sensitvity){
SwipeLeft();
}else if((e2.getX() - e1.getX()) > sensitvity){
SwipeRight();
}

return true;
}

};
GestureDetector gestureDetector= new GestureDetector(simpleOnGestureListener);

谢谢。

编辑:

main.xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<ViewFlipper
android:id="@+id/ViewFlipper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >

<include
android:layout_height="match_parent"
layout="@layout/mymain" />

<include layout="@layout/secondmain" />
<include layout="@layout/thirdmain" />
<include layout="@layout/fourthmain" />
</ViewFlipper>
</LinearLayout>

Edit2:我所有包含的布局都实现了 ScrollView 。滚动是否有可能接受这些事件并处理它们?如果是,如何检测手势?

最佳答案

我找到了一个完美的解决方案。我实现了新方法:

@Override
public boolean dispatchTouchEvent(MotionEvent event) {

View v = getCurrentFocus();
boolean ret = super.dispatchTouchEvent(event);

现在一切正常!

编辑:

我的最终代码:

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
View v = getCurrentFocus();
if (v instanceof EditText) {
View w = getCurrentFocus();
int scrcoords[] = new int[2];
w.getLocationOnScreen(scrcoords);
float x = event.getRawX() + w.getLeft() - scrcoords[0];
float y = event.getRawY() + w.getTop() - scrcoords[1];
if (event.getAction() == MotionEvent.ACTION_UP
&& (x < w.getLeft() || x >= w.getRight() || y < w.getTop() || y > w
.getBottom())) {

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus()
.getWindowToken(), 0);
}
}
boolean ret = super.dispatchTouchEvent(event);
return ret;
}

关于android - 方法 onTouchEvent 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13083871/

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