Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android停止发送onTouch事件_Android_Touch Event_Ontouchlistener - Fatal编程技术网

Android停止发送onTouch事件

Android停止发送onTouch事件,android,touch-event,ontouchlistener,Android,Touch Event,Ontouchlistener,我有一个自定义视图,希望为其处理触摸事件。特别是,在视图上拖动手指会导致其内容滚动。为此,我实现了以下onTouchListener: private OnTouchListener graphOnTouchListener = new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { System.out.println("onTouch called"); S

我有一个自定义视图,希望为其处理触摸事件。特别是,在视图上拖动手指会导致其内容滚动。为此,我实现了以下onTouchListener:

private OnTouchListener graphOnTouchListener = new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
        System.out.println("onTouch called");
        System.out.println("view width: " + v.getWidth());
        System.out.println("view height: " + v.getHeight());
        System.out.println("view: " + v);

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                // First finger down
                System.out.println("ACTION_DOWN x: " + event.getX() + ", y: " + event.getY());
                startTouch(event.getX());
                break;
            case MotionEvent.ACTION_UP:
                // Last finger to be removed
                System.out.println("ACTION_UP x: " + event.getX() + ", y: " + event.getY());
                break;
            case MotionEvent.ACTION_MOVE:
                // A finger moves
                System.out.println("ACTION_MOVE x: " + event.getX() + ", y: " + event.getY());
                moveTouch(event.getX());
                break;
            default:
                break;
        }  

        // The event has been handled, so return true
        return true;
    }
};
除非用户在视图中向上或向下移动手指,否则这将按预期工作。此时,触摸事件停止发送(如LogCat中缺少输出所示)。但是,使用
adb shell getevent
仍显示触摸移动生成的事件

有人能建议我如何纠正这个问题吗

根据要求,布局XML如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#ffffff">
    <FrameLayout android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ScrollView android:layout_width="fill_parent"
            android:layout_height="fill_parent" android:padding="10dip">
            <LinearLayout android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:orientation="vertical">
                <com.graphlib.ShaderAreaGraphView
                    android:layout_width="fill_parent" android:layout_height="200dip"
                    android:id="@+id/activity_power_realtime" android:background="#000000" />
                <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:gravity="center">
                    <Button android:text="&lt;&lt;" android:id="@+id/goBackwardsButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
                    <Button android:text="Zoom out" android:id="@+id/zoomOutButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
                    <Button android:text="Zoom in" android:id="@+id/zoomInButton" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button>
                    <Button android:text="&gt;&gt;" android:id="@+id/goForwardsButton" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
                </LinearLayout>
            </LinearLayout>
        </ScrollView>
    </FrameLayout>
</LinearLayout>

您需要在每种情况下编写break语句。否则,所有案件都将被执行

        case MotionEvent.ACTION_DOWN:
            // First finger down
            System.out.println("ACTION_DOWN x: " + event.getX() + ", y: " + event.getY());
            startTouch(event.getX());
            break;
        case MotionEvent.ACTION_UP:
            // Last finger to be removed
            System.out.println("ACTION_UP x: " + event.getX() + ", y: " + event.getY());
            break;
        case MotionEvent.ACTION_MOVE:
            // A finger moves
            System.out.println("ACTION_MOVE x: " + event.getX() + ", y: " + event.getY());
            moveTouch(event.getX());
            break;

或者你可以把它放进if。。。否则,如果开关底部出现条件,请尝试添加

default:
    break;

可能不是修复方法,但最好是这样做。

问题是视图包含在滚动视图中。删除ScrollView可解决此问题。

解决此问题的简单方法是创建对滚动视图的引用,例如myScrollView=(ScrollView)findViewById(R.id.myScrollView)。然后在onTouchEvent中,操作处于关闭状态:

myScrollView.RequestDisallowWinterCeptTouchEvent(true)

而在行动中!

myScrollView.RequestDisallowWinterCeptTouchEvent(false)


请向我们展示布局xml。如果我理解正确,行动是他们移除手指吗?你不想休息一下吗在那之后,以确保在他们取下手指后不会触发动作?是的,可能吧,但我对此不太在意。无论如何,我都会编辑代码以阻止分心。这没有效果,但我还是编辑了原始代码。你面临的问题我测试你的听众它工作得很好给我坐标向下、移动和向上时间向左和向右拖动一点,然后向上移动。在此之后,向左和向右移动将不会创建事件(至少不会在我正在测试它的NexusOne或emulator上)。不幸的是,这并没有修复任何问题。我已根据您的建议更新了代码。正确,我的自定义视图处于垂直滚动视图中,onTouch事件在自定义视图中水平运行,但在垂直移动手指时停止。