触摸视图的事件在彼此之上

时间:2014-01-10 10:02:16

标签: android

我正在尝试创建一个布局,其中两个视图(在本例中为ImageButtons)完全相互重叠。两者都应该处理触摸事件以切换按钮的alpha。

然而,只有顶部的按钮才能接收触摸事件(但由于alpha为0,因此不执行任何操作)。当我按下按钮时,buttonBelow不会收到任何Touch事件。

我用来创建视图的代码和OnTouchListeners:

        // Create 2 Buttons on top of each other
        final ImageButton buttonBelow = new ImageButton(getActivity());//ImageButton
        buttonBelow.setImageResource(R.drawable.ic_action_locate);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        buttonBelow.setLayoutParams(layoutParams);

        final ImageButton buttonAbove = new ImageButton(getActivity());//ImageButton
        buttonAbove.setImageResource(R.drawable.ic_action_delete);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        buttonAbove.setLayoutParams(params);
        buttonAbove.setAlpha(0.0f);

        root.addView(buttonBelow);
        root.addView(buttonAbove);

        buttonBelow.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP && v.getAlpha() > 0) {
                    Log.e("Button", "Pressed below");
                    buttonBelow.setAlpha(0.0f);
                    buttonAbove.setAlpha(1.0f);
                    return true;
                }
                return false;
            }
        });

        buttonAbove.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP && v.getAlpha() > 0) {
                    Log.e("Button", "Pressed above");
                    buttonBelow.setAlpha(1.0f);
                    buttonAbove.setAlpha(0.0f);
                    return true;
                }
                return false;
            }
        });

buttonBelow如何接收触摸事件?

3 个答案:

答案 0 :(得分:1)

让你的类实现View.OnTouchListenerYou,并为两个buttoms使用相同的监听器,然后在其中做出决定:

class YourClass implements View.OnTouchListener {

    public void onCreate(Bundle savedInstanceState) {



        //...

           buttonBelow.setOnTouchListener(this);

           buttonAbove.setOnTouchListener(this);

        }

        //...

    public boolean onTouch(View v, MotionEvent event) {
          if (event.getAction() == MotionEvent.ACTION_UP) {
            if (buttonBelow.getAlpha() > 0) {
              buttonBelow.setAlpha(0.0f);
              buttonAbove.setAlpha(1.0f);

            }
            else {
              buttonBelow.setAlpha(1.0f);
              buttonAbove.setAlpha(0.0f);

           }


         return true;
         }
     return false;
   }

}

答案 1 :(得分:0)

您可以删除两个视图(ImageButton)并以相反的顺序将其再次添加到相对布局

答案 2 :(得分:0)

你不能这样做,因为button above就像阻挡下面那个 但是你可以使用x和y

在上面的顶部与顶部相对
buttonAbove.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP && v.getAlpha() > 0) {
                Log.e("Button", "Pressed above");
                int y = event.getY();
            int x = event.getX();
                buttonBelow.setAlpha(x);
                buttonAbove.setAlpha(y);
                return true;
            }
            return false;
        }
    });

或者在y中同时使用if语句但在触摸单击中单击如下:

                if(y > 10)
                buttonBelow.setAlpha(y);
                buttonAbove.setAlpha(0);}
                else{
                buttonBelow.setAlpha(x);
                buttonAbove.setAlpha(y)
                  }

但最好使用那里的那个