2014-08-28 59 views
0

我有幾個seekbars作爲兒童的一個LinearLayout,在SeekBars我使用android:clickable=false和上的LinearLayout我有android:clickable=true和所定義的但是任何按下相應setOnClickListennerSeekBars不會觸發LinearLayout點擊,我已經閱讀了某處,它應該通過將SeekBars替換爲Buttons並將clickable屬性設置爲false並將其點擊冒泡到父級來進行測試。 我的問題是爲什麼行爲不相似?的ViewGroup(的LinearLayout)和兒童onClick事件不傳遞到的ViewGroup

編輯:添加代碼示例

佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MyActivity"> 

<LinearLayout 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <SeekBar 

     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:clickable="false" 
     android:longClickable="false" 
     android:focusable="false" 
     /> 

    <Button 
     android:layout_marginTop="10dp" 
     android:clickable="false" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Click me!"/> 


    </LinearLayout> 

活動:

public class MyActivity extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my); 


    ViewGroup container = (ViewGroup) findViewById(R.id.container); 
    container.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(MyActivity.this, "Container was clicked", Toast.LENGTH_SHORT).show(); 
     } 
    }); 




} 
} 
+0

嗯,你在代碼中缺少這個:「並且在LinearLayout上我有'android:clickable = true'」 – Darrell 2014-08-28 13:47:30

+0

10是的,我發佈的是一個使用setOnClickListener的例子,它的不相關行爲是相同的,甚至與在單擊「LinearLayout」時沒有'android:clickable = true',在單擊SeekBar時調用監聽器,而不是在調用該事件的Button上執行。 – forcewill 2014-08-28 13:51:46

+0

沒關係,所以當你點擊按鈕時它工作嗎?當你點擊搜索欄時它不起作用?正確? – Darrell 2014-08-28 13:53:36

回答

0

擁有終於明白你問什麼,我認爲這將解決這個問題。 SeekBar不是可調焦的,而Button是。

這是你從SeekBar刪除此行,它不會工作的原因:

android:focusable="false" 

允許這要成爲焦點應該使雙方的ButtonSeekBar功能以同樣的方式被點擊時。

+0

我可能以困惑的方式問了問題或你誤解我知道設置'可點擊'到'假'使得程序忽略那些我想在SeekBar上發生的點擊我不想點擊它,事件會冒泡到已定義點擊事件的父級(LinearLayout)。 – forcewill 2014-08-28 13:28:17

+0

你能發佈你的代碼嗎? – Darrell 2014-08-28 13:30:54

+0

我已經添加了代碼和xml佈局 – forcewill 2014-08-28 13:43:24