2013-10-30 69 views
0

我有一個線性佈局和一些子視圖的活動。如果我用一個手指觸摸子視圖外部,然後用另一個手指觸摸子視圖,子視圖上不會觸發任何觸摸事件。如果我直接觸摸子視圖,我會得到一個事件。多點觸摸式子視圖無法獲取事件

我重寫了活動中的觸摸事件並調用了子視圖,但這種方法有效,但X,Y來自父項,而不是相對於子項。不管其他手指觸摸到外面,孩子如何看待圖框佈局,獲得所有的觸摸事件?

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    if (event != null && clock != null) { 
     event.offsetLocation(-frameLayout1.getLeft(),- frameLayout1.getTop()); 
     clock.onTouchEvent(event); 

    } 
    return false; 
}; 

佈局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
android:layout_width="fill_parent" android:layout_height="fill_parent"> 

    <LinearLayout 
     android:id="@+id/LearningLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <FrameLayout 
      android:id="@+id/frameLayout1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> 
     </FrameLayout> 

     <TextView 
      android:id="@+id/targetTime" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:gravity="center_horizontal" 
      android:text="TextView" android:textSize="64dp" android:layout_gravity="center_horizontal"/> 

     <TextView 
      android:id="@+id/textualTime" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:gravity="center_horizontal" 
      android:text="TextView" android:textSize="64dp" android:layout_gravity="center_horizontal"/> 


     <TextView 
      android:id="@+id/textualTime2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:gravity="center_horizontal" 
      android:text="TextView" android:textSize="64dp" android:layout_gravity="center_horizontal"/> 

    </LinearLayout> 


    <com.google.ads.AdView android:id="@+id/adView" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          ads:adUnitId="a14e531e444c51a" 
          ads:adSize="SMART_BANNER" android:layout_marginTop="5dp" android:layout_alignParentBottom="true"/> 

</RelativeLayout> 
+0

爲孩子計算xy是否能解決您的問題? – hasan83

+0

我嘗試過使用'event.offsetLocation(-clock.getLeft(), - clock.getHeight());',這沒有奏效。沒有直接調用childView onTouchEvent,有沒有更清晰的方法? – tsukimi

+0

正在尋找X和Y來確定哪個視圖被點擊? – hasan83

回答

0

基本上clock.getLeft()和clock.getTop()正在返回0 .The時鐘的的FrameLayout內部的視圖subclasss。如果我使用framelayout getLeft()和getTop()它將返回正確的偏移量並且工作。 有沒有比調用childview touch事件和抵消更好的方法?