【问题标题】:how to deal with the onclick event when two view overlapping两个视图重叠时如何处理onclick事件
【发布时间】:2012-02-29 14:13:42
【问题描述】:

我使用 layoutparams 来设置几个视图的位置,有些是重叠的。当我触摸重叠部分时,包括重叠部分的两个视图都对应于触摸事件!但我只想与上面的对应,任何人都可以帮助我吗?感谢您的回答!

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( 50, 50 );
    params.Left_AlignMargin = 100;
    params.Top_AlignMargin = 100;
    relativeLayout.addView( imgeView, params );

    params = new RelativeLayout.LayoutParams( 50, 50 );
    params.Left_AlignMargin = 50;
    params.Top_AlignMargin = 100;
    relativeLayout.addView( imageView2, params );
    //when i clicked the overlapped part, both imageview correspond to my action.how to deal with this ?

【问题讨论】:

  • 那么真正的问题是什么?如果碰到了错误的视图,什么也不做,就是这样。
  • 对不起,我已经解决了这个问题,我忘记关闭了!谢谢你的回答!
  • 你是怎么解决的?我对多个图像视图有类似的问题,其中图像在节点的一端相交。

标签: android android-view android-event


【解决方案1】:

您可以使用 View.OnTouchListener 作为顶视图上的触摸事件。当您这样做时,您可以将侦听器设置为返回 true(这意味着此触摸事件会消耗整个触摸事件)并且它应该防止第二个(底层)视图受到影响。 示例:

    topView.setOnTouchListener(new View.OnTouchListener() {


        @Override public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:


                    return true;
                case MotionEvent.ACTION_UP:{
                    //code for top view
                    return true;//this consumes the entire touch event

                }

            }
            return false;
        }
    });

【讨论】:

    猜你喜欢
    • 2016-05-28
    • 2014-12-08
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    相关资源
    最近更新 更多