我创建了一个类MapView,它绘制了一些表示地图的圆圈和线条,您可以触摸一个圆圈以更改其颜色。如果按照它应该的方式工作,但是当我将视图添加到布局时,touchlistener不会返回任何接触。当视图位于布局中时,onTouchEvent不起作用
这里是我的MainActivity.java
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.*;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
MapView mapView;
RelativeLayout mainLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainLayout = new RelativeLayout(this);
mapView = new MapView(this);
mapView.findViewById(R.id.theMap2);
mapView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
float x = event.getX();
float y = event.getY();
System.out.println("Touch! X: " + x + ", Y: " + y);
mapView.touched(x, y);
return true;
}
return true;
}
});
setContentView(R.layout.activity_main2);
}
我可以看到的MapView的工作原理是这样:setContentView(mapView);
这里是activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<bus.com.ece602_bus.MapView
android:id="@+id/theMap2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
我在做什么错?为什么没有注册触摸?
你为什么试图找到视图('mapView.findViewById(R.id.theMap2);')但不使用它? – maysi 2015-02-10 19:03:28