android应用程序未注册onTouch

android应用程序未注册onTouch,android,android-layout,ontouchlistener,android-event,Android,Android Layout,Ontouchlistener,Android Event,这是我的布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivit

这是我的布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp" />

    <!-- will add other views later -->

</RelativeLayout>
这是主要活动

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        View myView = new MyView(this, data);
        FrameLayout container = (FrameLayout) findViewById(R.id.container);
        container.addView(myView);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
问题


应用程序未注册自定义视图的my touch事件。该视图显示了onDraw的所有内容,但当我单击时,onTouch未注册。我尝试在onTouch中的
if
处设置一个断点,但始终未达到该断点。

可能是您没有在构造函数中将onTouchListener注册到视图中,我不太确定,但您是否像onDraw那样为视图重写了一个方法,也许这更适合您

@Override
public boolean onTouchEvent (MotionEvent event){

return true;

}

我认为需要调用setOnTouchListener()来注册touch事件的回调

@Override
public boolean onTouchEvent (MotionEvent event){

return true;

}