2012-12-11 16 views
3

我想創建一個形狀或按鈕,但當它被點擊時,它會將事件發送到它後面的視圖。 我已經使用onInterceptTouchEvent,但事件是由按鈕而不是視圖。 這是我的代碼:如何在Android中點擊按鈕背後的視圖?

<?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; 
    } 

} 

感謝

+0

您是否嘗試在Button組件上以編程方式setOnclickListener(),然後在onClick方法上返回false。這使得onClick事件通過並繼續到其他組件後面? – smora

回答

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

在MultiTouchView類。 我希望這個作品

+0

不,仍然在做任何事情,當點擊按鈕:( – Damian

0

您應該擴展按鈕,並在Button.onTouchEvent返回false()

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

或通知後面的按鈕查看的OnTouchListener:

+0

嗨,我已經創建 – Damian

+0

該按鈕不工作,我創建了ButtonDummy與onTouchEvent返回false,但我仍然有同樣的問題。 – Damian

+0

是否如果你設置按鈕可見性工作,工作? –

0

我已經解決了這個問題

機器人:在按鈕添加此點擊=「假」

感謝您的幫助

0

您可以在按鈕的大小和位置上創建一個Rectangle變量,並像使用它一樣;

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

如果在更新按鈕之前更新該矩形,矩形將成爲新的不可見按鈕。

相關問題