Android图像和按钮

时间:2011-12-25 19:47:20

标签: android image button

如果我想在图像上设置按钮,我应该使用什么?我的意思是,能够点击图像的某些点并查看一些信息。


如果我想在这些点上显示文字怎么办?想象一下,我有一张邻居的地图,我希望显示一些信息(例如我的家)。如果有人按下我家,他可以看到更多细节,比如我的名字,手机等等。图像(也就是地图)将是稳定的,它不会像背景那样改变。

2 个答案:

答案 0 :(得分:2)

您必须从界面onTouch实施方法OnTouchListener。例如:

OnTouchListener listener = new OnTouchListener() {
    @Override
    public false onTouch(View v, MotionEvent event) {
        if(!event.getAction() == MotionEvent.ACTION_DOWN)
            return false; //If the touch event was not putting the finger down on the screen, return false(Actions may be move, up, and so on)
        final float x = event.getX();
        final float y = event.getY();
        if(x and y are in some range)
            doFirstThing();
        else if(x and y are in another range..)
            doSecondThing();
        //... and so on...
        //In the end, you must return a boolean saying whether you "consumed" the event - if you handled the event or not.
    }
}

并且不要忘记将听众设置为OnTouchListener的{​​{1}}!

ImageView

答案 1 :(得分:0)

我相信你想要实现的目标,你不需要使用按钮。相反,创建一个自定义视图,在onDraw方法中绘制图像并按照Jong建议处理onTouch事件。例如,一旦您在家中检测到点击,您就可以创建另一个包含相关信息的视图并将其显示给用户。

有关如何创建自定义视图的信息,您可以参考this queestion。谷歌为它做准备