2015-06-10 16 views
0

我想從整個屏幕收集所有可能的接觸。我實現了一個收集MainActivity的接觸

@Override 
    public boolean onTouchEvent(MotionEvent e) 
    { 
     double x = e.getX(); 
     double y = e.getY(); 
    } 

但現在我添加了一個TableView中的「場景」,當我點擊它上面的方法不會被調用,因爲TableView中正在吞噬潤色。 http://i.gyazo.com/d671768c45a0d9576d6ba3b822dfa85d.png

黃色區域周圍的一切都是可點擊的,但黃色區域本身不是。我希望它也是可點擊的,基本上我希望整個屏幕區域都是可點擊的,這樣我可以存儲觸摸位置。怎麼做?

編輯: onTouchEvent方法在MainActivity類中。 這裏是activity_main.xml中

<LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

    <ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <TableLayout 
      android:id="@+id/tableLayout1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:padding="10dp" 
      android:shrinkColumns="*" 
      android:stretchColumns="*" > 

      <TableRow android:layout_height="wrap_content"> 
       <TextView 
        android:id="@+id/column1" 
        android:text="tapcount" 
        android:layout_height="wrap_content" 
        /> 
      </TableRow> 

     </TableLayout> 
    </ScrollView> 
    </LinearLayout> 
+0

你在哪裏實施的onTouchEvent以及如何建立你的佈局文件? –

+0

onTouchEvent在MainActivity中,你的意思是「你的佈局文件是如何構建的?」檢查上面的activity_main.xml,我編輯了帖子。 – L3M0L

回答

0

蓋全視圖用自己透明的,就像這樣:

<RelativeLayout> 
    <LinearLayout 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent"> 
      <!--your content inside--> 
    </LinearLayout> 
    <View 
     android:id="@+id/cover" 
     android:layout_height="fill_parent" 
     android:layout_width="fill_parent"/> 
</RelativeLayout> 

然後使用id =蓋用你的代碼View並在onTouchEvent返回總是false爲在doc中:

返回 如果處理事件,則爲true,否則爲false。

false硬編碼MotionEvent會在你onCreate()方法傳遞到下面的意見/

0

下嘗試執行這樣的事情

View contentView = (View) findViewById(R.id.linearLayout1); //Your layout 
     contentView.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       if(event.getAction() == MotionEvent.ACTION_DOWN){ 

        Toast.makeText(getApplicationContext(), event.getX() + " " + event.getY(), Toast.LENGTH_SHORT).show(); 
        return true; 
       } 
       return false; 
      } 
     }); 
+0

這個解決方案不適合我:/不知道爲什麼,但onTouch事件沒有被觸發。 – L3M0L

+0

你把監聽器放在你的底座佈局上嗎?我在我的一個應用程序中測試過它,它似乎工作得很好 – Galax