简体   繁体   English

OnGenericMotionListener似乎不起作用

[英]OnGenericMotionListener doesn't seem to be working

This is really weird all other listeners work like onClick etc.. but this listener doesnt seem to be working, heres my code: 这真的很奇怪,所有其他监听器都像onClick等一样工作。但是此监听器似乎不起作用,这是我的代码:

public class HeloActivity extends Activity implements OnGenericMotionListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        View root = findViewById(R.id.root  );
        root.setOnGenericMotionListener(this);
    }

    @Override
    public boolean onGenericMotion(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        Log.d( "special",v.toString() ); 
        return false;
    }
}

why is this not working? 为什么这不起作用?

This is an old question, but I'm currently looking at the same thing so I'll add this for future reference. 这是一个老问题,但是我目前正在研究同一件事,因此我将其添加以供将来参考。

It might be because Activity implements an onGenerericMotionEvent method which receives motion events, but you're also implementing the method from View - onGenericMotion in the same class. 可能是因为Activity实现了一个接收运动事件的onGenerericMotionEvent方法,但是您也正在同一类中的View - onGenericMotion中实现该方法。

The Android docs say to implement one or the other. Android文档说要实现其中一个。

I've not tried how you're doing it, but I know it works by using a different class that implements OnGenericMotionListener and using setOnGenericMotionListener on the root view. 我没有尝试过如何做,但是我知道它可以通过使用实现OnGenericMotionListener的其他类并在根视图上使用setOnGenericMotionListener来工作。

You do realize that you both implements the listener and then attach it to the view? 您确实意识到自己都实现了侦听器,然后将其附加到视图? At the very least it is redundant.... 至少它是多余的。

Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM