gpt4 book ai didi

android - onTouchEvent 不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 11:18:45 27 4
gpt4 key购买 nike

我正在使用 MapView 创建 Android 应用程序。我需要对用户的触摸事件使用react。我尝试使用 dispatchTouchEventonTouchEvent 中处理一些操作。但是这个 onTouchEvent 没有被调用!当我在 dispatchTouchEvent 中添加此代码时:

      MarkerOverlay myMarker = new MarkerOverlay(marker,edTxtAlarmClockName.getText().toString());        
mapView.getOverlays().add(myMarker);
mapView.invalidate();

onTouchEvent 被调用。为什么会这样以及如何描述 overlay 处理程序?

public class MainActivity extends MapActivity implements OnTouchListener{

private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private MyLocationOverlay myLocationOverlay;
private Context context;
public GeoPoint lastClickedPoint;
Dialog dialog;
EditText edTxtAlarmClockName;
ToggleButton tbOnOffAlarmClock;
boolean reClick ;
private int lastTouchX, lastTouchY;
private boolean hasMoved = false;
Button btnOk;
Button btnCancelDelete;
Button btnAlarmClockStop;
public int distance=50;
private int indexOfSelectedOverlay;

public void onCreate(Bundle bundle) {
super.onCreate(bundle);

setContentView(R.layout.activity_main);
context = MainActivity.this;

// Configure the Map
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
mapView.setSatellite(true);
mapView.setOnTouchListener(this);

mapController = mapView.getController();
mapController.setZoom(15);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10, geoupdate);

myLocationOverlay = new MyLocationOverlay(this, mapView);
myLocationOverlay.disableCompass();
myLocationOverlay.enableMyLocation();
mapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mapView.getController().animateTo(myLocationOverlay.getMyLocation());
mapView.setBuiltInZoomControls(true);
}
});


final Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
dialog = new Dialog(context);
dialog.setContentView(R.layout.custom1);
dialog.setTitle(R.string.str_new_alarm);
tbOnOffAlarmClock=(ToggleButton)dialog.findViewById(R.id.toggleButton1);
edTxtAlarmClockName = (EditText) dialog.findViewById(R.id.textView1);
edTxtAlarmClockName.setText(R.string.str_name);
btnOk = (Button) dialog.findViewById(R.id.btn_ok);
btnCancelDelete = (Button) dialog.findViewById(R.id.btn_cancel);
btnOk.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
MarkerOverlay myMarker;
if (reClick) //click on the overlay
{//search and rename
myMarker=(MarkerOverlay)mapView.getOverlays().get(indexOfSelectedOverlay);
mapView.getOverlays().remove(indexOfSelectedOverlay);
myMarker.setName(edTxtAlarmClockName.getText().toString());
mapView.getOverlays().add(myMarker);
mapView.invalidate();
}
else
{//creat overlay
myMarker = new MarkerOverlay(drawable,edTxtAlarmClockName.getText().toString());
myMarker.setName(edTxtAlarmClockName.getText().toString());
if (tbOnOffAlarmClock.isChecked()) {
myMarker.active=true;
} else {
myMarker.active=false;
}
mapView.getOverlays().add(myMarker);
mapView.invalidate();
}
dialog.dismiss();
}
});


btnCancelDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
OverlayItem item;
Point itemPoint,currentPoint;
MarkerOverlay myOverlay;
if (reClick) //delete marker
{
Log.d("debug","delete");
mapView.getOverlays().remove(indexOfSelectedOverlay);
mapView.invalidate();

}
//else CANCEL
dialog.dismiss();
}
});

}


@Override
protected boolean isRouteDisplayed() {
return false;
}

@Override
protected void onDestroy() {
super.onDestroy();
locationManager.removeUpdates(geoupdate);
}


@Override
protected void onResume() {
super.onResume();
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
}

@Override
protected void onPause() {
super.onPause();
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
}

public class MarkerOverlay extends ItemizedOverlay<OverlayItem>
{

private Drawable marker = null;
private boolean active=true;
private GeoPoint pointOfOverlay;
private String customName;
public List<OverlayItem> items=new ArrayList<OverlayItem>();;
public MarkerOverlay(Drawable marker,String name)
{
super(boundCenterBottom(marker));
this.marker=marker;
OverlayItem item = new OverlayItem(lastClickedPoint, name,"");
this.pointOfOverlay=lastClickedPoint;
this.customName=name;
items.add(item);
populate();

}


public String getName()
{
return customName;
}

public void setName(String name)
{
customName=name;
}
public GeoPoint getPoint()
{
return pointOfOverlay;
}

@Override
protected OverlayItem createItem(int i)
{
return(items.get(i));
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
super.draw(canvas, mapView, shadow);
boundCenterBottom(marker);
}

@Override
public int size()
{
return(items.size());
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent, MapView mapView) {
boolean consumed = super.onTouchEvent(motionEvent,mapView);
int action = motionEvent.getAction();
int size;
switch (action) {
case MotionEvent.ACTION_DOWN:
lastTouchX =(int) motionEvent.getX();
lastTouchY =(int) motionEvent.getY();
hasMoved = false;
break;
case MotionEvent.ACTION_MOVE:
hasMoved = moved(motionEvent);

break;
case MotionEvent.ACTION_UP:
if (!moved(motionEvent))
{
size = items.size();
if (size > 0)
{
OverlayItem item = items.get(size - 1);
Point p1 = mapView.getProjection().toPixels(item.getPoint(), null);
if (hitTest(item, marker, lastTouchX - p1.x, lastTouchY - p1.y))
{
dialog.setTitle(item.getTitle());
btnCancelDelete.setText(R.string.str_delete);
dialog.show();
reClick=true;
}
else
{
edTxtAlarmClockName.setText(R.string.str_name);
btnCancelDelete.setText(R.string.str_cancel);
dialog.show();
lastClickedPoint = mapView.getProjection().fromPixels((int) motionEvent.getX(), (int) motionEvent.getY());
reClick=false;
}
populate();
return true;
}
else
{
edTxtAlarmClockName.setText(R.string.str_name);
dialog.setTitle(R.string.str_new_alarm);
btnCancelDelete.setText(R.string.str_cancel);
dialog.show();
lastClickedPoint = mapView.getProjection().fromPixels((int) motionEvent.getX(), (int) motionEvent.getY());
reClick=false;
}
populate();
return true;
}
}

return false||consumed;
}


private boolean moved(MotionEvent evt) {
Log.d("debug", "ontouchevent move");
return hasMoved ||
Math.abs(evt.getX() - lastTouchX) > 20.0 ||
Math.abs(evt.getY() - lastTouchY) > 20.0;
}
}


@Override
public boolean onTouch (View v, MotionEvent event)
{
int action = event.getAction();

if (action == MotionEvent.ACTION_UP)
{
lastClickedPoint= mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY());
return true;
}
return false;

}
@Override
public boolean dispatchTouchEvent(MotionEvent motionEvent) {
int action = motionEvent.getAction();
Drawable marker = getResources().getDrawable(R.drawable.ic_launcher);
switch(action) {
case MotionEvent.ACTION_UP:
MarkerOverlay myMarker = new MarkerOverlay(marker,edTxtAlarmClockName.getText().toString());
mapView.getOverlays().add(myMarker);
mapView.invalidate();
break;
}
return super.dispatchTouchEvent(motionEvent);
}

}

最佳答案

我觉得应该把onTouchEvents的过程加入on touch中,同时让MapView作为一个全局变量,然后在覆盖的onTOuch事件中调用Mapview变量。

据我所见,没有连接发起onTouchEvents

关于android - onTouchEvent 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12517205/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com