OnGenericMotionListener似乎不起作用

时间:2011-12-08 19:03:47

标签: java android

这真是奇怪所有其他听众的工作就像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;
    }
}

为什么这不起作用?

2 个答案:

答案 0 :(得分:0)

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.

The Android docs say to implement one or the other.

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.

答案 1 :(得分:-1)

您是否意识到您既实现了侦听器又将其附加到视图?至少它是多余的......

谢谢。