如何在Android中点击按钮后面的视图?

3

我想创建一个形状或按钮,但当它被点击时,它将把事件发送到它后面的视图。 我已经使用了onInterceptTouchEvent,但是事件仍然被按钮接受,而不是View。 这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01" 
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent" 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:background="@drawable/background">

 <com.Example.MultitouchView
          android:id="@+id/touchView"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="#FF0000" />

 <Button
    android:id="@+id/btnDo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bg1"
    android:text="Test"
    android:textSize="20sp"
    android:textColor="#110342" >
</Button>

</RelativeLayout>

MultitouchView.class

public class MultitouchView extends View {

private boolean isMultiTouch = false;
private static final int        SWIPE_MIN_LENGHT        = 100;
private static final long       MULTITOUCH_DELAY        = 500;
private long    mTouchStart;

public MultitouchView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public MultitouchView(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);

  }

  public MultitouchView(Context context, AttributeSet attrs) {
          super(context, attrs);

  }

  public boolean onInterceptTouchEvent(MotionEvent event){
    Toast toast = Toast.makeText(getContext(), "TEST", 1000);
    toast.show();

    return false;

  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        int action = event.getAction() & MotionEvent.ACTION_MASK;
        switch (action) {
            case MotionEvent.ACTION_DOWN: {
                Log.d("MultitouchExample","Action Down");
                mTouchStart = System.currentTimeMillis();
                SoundManager.playSound(1,1);
                break;
            }
        }
        return true;
  }

}

谢谢


你是否尝试在Button组件上以编程方式设置setOnclickListener(),然后在onClick方法中返回false。这样可以使onClick事件继续传递并继续触发其他位于其后面的组件吗? - smora
4个回答

0
@Override 
    public boolean hasWindowFocus() {
            return isEnabled() && getVisibility() == VISIBLE ? true : false;
        }

在你的MultiTouchView类中。 希望这个能够工作。

0

你应该扩展Button类并在Button.onTouchEvent()方法中返回false。

new Button(dozActivity) {
@Override
public boolean onTouchEvent(MotionEvent event)
{
    return false;
}
};

或者通知按钮后面的View的OnTouchListener:


按钮无法工作,我已经创建了ButtonDummy,并在onTouchEvent中返回false,但仍然存在相同的问题。 - Damian

0

我通过在按钮中添加以下代码解决了这个问题:

android:clickable="false"

感谢您的帮助。


0

你可以根据按钮的大小和位置创建一个矩形变量,并像这样使用它:

If(touch != null && rectangle.contains(touchPoint))
{
    // do action      
}  

如果您在更新按钮之前更新此内容,则矩形将成为您的新不可见按钮。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接