2013-01-17 33 views
0

我創建了自己的小部件,一個帶有TextView(標籤)和微調控件的LinearLayout。將單擊事件傳遞給Android微調器

它看起來像這樣的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:orientation="horizontal" 
    android:clickable="true" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    style="@style/MySpinner> 
    <TextView android:id="@+id/label" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:textStyle="bold" 
     android:layout_weight="0.4" 
     android:textSize="18sp" 
     android:padding="8dp" 
     android:enabled="false"/> 
    <Spinner android:id="@+id/spinner" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:entries="@array/thats_me_age_values" 
     android:layout_weight="0.6" 
     android:spinnerMode="dialog" 
     android:padding="8dp"/> 
</LinearLayout> 

的LinearLayout中應該得到所有觸摸事件,因此它可以正確地使用它的風格(背景變化對壓)。樣式將背景屬性設置爲此選擇器:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- pressed --> 
    <item android:state_pressed="true" android:state_focused="false"> 
     <shape android:shape="rectangle" android:visible="true"> 
      <corners android:topLeftRadius="7dp" android:topRightRadius="7dp"/> 
      <solid android:color="@color/dark_yellow"/> 
      <stroke android:width="1dp" android:color="@color/grey"/> 
     </shape> 
    </item> 
    <!-- focused --> 
    <item android:state_focused="true"> 
     <shape android:shape="rectangle" android:visible="true"> 
      <corners android:topLeftRadius="7dp" android:topRightRadius="7dp"/> 
      <solid android:color="@color/dark_yellow"/> 
      <stroke android:width="1dp" android:color="@color/grey"/> 
     </shape> 
    </item> 
    <!-- default --> 
    <item> 
     <shape android:shape="rectangle" android:visible="true"> 
      <corners android:topLeftRadius="7dp" android:topRightRadius="7dp"/> 
      <solid android:color="@color/yellow"/> 
      <stroke android:width="1dp" android:color="@color/grey"/> 
     </shape> 
    </item> 
</selector> 

我試着重寫LinearLayout的onInterceptTouch方法。日誌消息顯示爲按下,但背景不變。

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    Log.i("TOUCH", "onTouchEvent"); 
    return super.onTouchEvent(event); 
} 

@Override 
public boolean onInterceptTouchEvent(MotionEvent ev) { 
    onTouchEvent(ev); 
    Log.i("TOUCH", "onInterceptTouchEvent"); 
    return false; 
} 
+0

您是否嘗試在'LinearLayout'上設置'onClickListener'? – seaplain

+0

我做到了。它只在觸摸8dp填充時觸發。 – Klaasvaak

+0

我可以考慮一堆可以嘗試的東西,比如在'FrameLayout'中放置'LinearLayout'並在頂部放置一個清晰的'LinearLayout',但黑客永遠不會是最好的解決方案,我甚至不知道事情就像那樣會工作 – seaplain

回答

0

我想加入的android:addStatesFromChildren =「true」以將LinearLayout中解決您的問題......所以,當你點擊/集中你的微調線性佈局將改變其狀態,以點擊/聚焦。

+0

謝謝,這有效!儘管如此,仍然必須重寫onInterceptTouchEvent並調用onTouchEvent。 – Klaasvaak

+0

你確定你需要重寫它們嗎?我測試了一下...在wat情況下它不工作? – Leonidos

+0

如果我不覆蓋onInterceptTouchEvent,按下後不會更改背景。 – Klaasvaak

相關問題