布尔方法返回值的问题
我有这个函数可以在点击标记时显示 AlertDialog:
protected boolean onTap(int index) {
db = openHelper.getWritableDatabase();
AlertDialog.Builder dialog = new AlertDialog.Builder(Geo.this);
String[] columns_descri = new String[] {COL_DESCRI};
Cursor cur = db.query(true, TABLE_COORD, columns_descri,null, null, null, null, null, null);
if (cur.moveToPosition(index-1)) {
String description = cur.getString(cur.getColumnIndexOrThrow(COL_DESCRI));
dialog.setTitle("Info.");
dialog.setMessage(description);
dialog.show();
}
db.close();
return true; }
这个方法可以检索地图上任何点击的触摸:
@Override
public boolean onTouchEvent(MotionEvent event, MapView maMap)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = maMap.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(getBaseContext(),
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
}
return true;
}
问题是当 onTouchEvent
返回 TRUE 时,我无法控制地图、移动它或点击标记时显示警报对话框。当它为假时,我重新控制地图,但吐司显示很多次(5或6次)。 这个奇怪问题的根源可能是什么?
I have this function to show an AlertDialog when taping on marker:
protected boolean onTap(int index) {
db = openHelper.getWritableDatabase();
AlertDialog.Builder dialog = new AlertDialog.Builder(Geo.this);
String[] columns_descri = new String[] {COL_DESCRI};
Cursor cur = db.query(true, TABLE_COORD, columns_descri,null, null, null, null, null, null);
if (cur.moveToPosition(index-1)) {
String description = cur.getString(cur.getColumnIndexOrThrow(COL_DESCRI));
dialog.setTitle("Info.");
dialog.setMessage(description);
dialog.show();
}
db.close();
return true; }
And this method to retrieve any tapped touch on map:
@Override
public boolean onTouchEvent(MotionEvent event, MapView maMap)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = maMap.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(getBaseContext(),
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
}
return true;
}
The problem is when onTouchEvent
returns TRUE i can't control the map, move it, or show the AlertDialog when taping on a marker. When it's false, i re-take control of the map, but the toast displays many time(5 or 6 times).
What could be the source of this weird problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
onTap 和 onTouch 事件的布尔值表示该操作是否已完全处理。如果返回 true,操作系统将停止级联到其他视图(因此您失去了对地图的控制)。当返回 false 时,操作系统将寻找其他可以处理触摸事件的方法。
The boolean value of the onTap and onTouch events represent whether the action was fully handled or not. If you return true, the OS will stop cascading to other views (hence why you lose control of the map). When you return false, the OS will look for other methods that can handle the touch event.
试试这个
try this
OnTouchEvent 在每个触摸事件上调用。
按下、拖动(许多事件)、释放。
因此你的吐司显示了很多。
仅在 if 子句中返回 true,否则返回 false。
那应该可以解决它。
OnTouchEvent is called on EACH touch event.
Press, drag (many events), release.
Therefore your toast is showing a lot.
return true only inside your if clause and return false otherwise.
That should solve it.