2012-11-09 58 views
1

我一直在試圖找到一個這個小時的解決方案,感覺就像我搜索了每一個關於stackoverflow的問題,並試圖在佈局中的一切。 在我嘗試下面顯示的佈局之前,edittext和兩個圖像按鈕位於scrollview內。在這個舊的佈局中,ontouch在我的片段中完美工作,但是一旦我用edittext和兩個圖像按鈕拉出相對佈局,ontouch就不會被觸發。有人可以幫我解決這個問題嗎?任何幫助表示讚賞。由於在RelativeLayout中使用ScrollView的片段> ontouch不起作用

View fragmentView = inflater.inflate(R.layout.request, container, false); 

fragmentView.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 

      } 
} 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="#BDCCE0" > 

    <EditText 
     android:id="@+id/etsearch" 
     android:layout_width="210dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="6dp" 
     android:layout_marginTop="10dp" 
     android:hint="search..." 
     android:singleLine="true" 
     android:imeOptions="actionDone" /> 

    <ImageButton 
     android:id="@+id/bsearch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/etsearch" 
     android:layout_marginTop="9dp" 
     android:src="@drawable/search" /> 

    <ImageButton 
     android:id="@+id/bupdaterequests" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_toRightOf="@id/bsearch" 
     android:layout_marginTop="9dp" 
     android:src="@drawable/refresh" /> 

    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:fillViewport="true" 
     android:background="#BDCCE0" 
     android:layout_below="@id/etsearch" > 

    <TableLayout 
     android:id="@+id/myTableLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" >  

    </TableLayout> 
</ScrollView> 
</RelativeLayout> 

回答

3

一個的TouchEvent開始在「最高」元素,其將逐漸「下降」的說法層次結構,直到它被一些事件所消耗。在這種情況下,滾動型,按鈕,或者所有的EditText消耗事件到達之前的RelativeLayout ...


基本上我只是想,當有人觸及編輯文本,鍵盤消失之外。

我通過簡單地使根佈局clickablefocusable解決這一點。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:clickable="true" 
    android:focusable="true" 
    android:focusableInTouchMode="true" > 
+0

所以我應該怎麼解決這個問題? – Max

+0

你想讓RelativeLayout的OnTouchListener做什麼?它會衝突其他視圖的基本功能嗎? – Sam

+0

基本上我只想當有人觸摸編輯文本之外的鍵盤消失。我有通常與觸摸一起工作的代碼,但由於ontouch不會觸發,我不知道如何解決此問題。 – Max

1

android:clickable="true"解決了這個問題..