布尔方法返回值的问题

发布于 2024-12-01 06:39:38 字数 1378 浏览 2 评论 0原文

我有这个函数可以在点击标记时显示 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别低头,皇冠会掉 2024-12-08 06:39:38

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.

傾城如夢未必闌珊 2024-12-08 06:39:38

试试这个

@Override
    public boolean onTouchEvent(MotionEvent event, MapView maMap) 
    {   
        //---when user lifts his finger---
        boolean result = false;
        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();
            result true;
        }                            
        //return (result | super.onTouchEvent(event, mapView));
        return result;
    }    

try this

@Override
    public boolean onTouchEvent(MotionEvent event, MapView maMap) 
    {   
        //---when user lifts his finger---
        boolean result = false;
        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();
            result true;
        }                            
        //return (result | super.onTouchEvent(event, mapView));
        return result;
    }    
梦在夏天 2024-12-08 06:39:38

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文