2017-02-09 42 views
0

我曾經在一個LinearLayout中有3 ImageButton的,問題是,點擊後,「@ ID /黃」調用第二個按鈕的功能,「@ ID /綠」調用第三個按鈕功能和「@ id/brown」來調用它自己的功能(它應該是這樣做的)。的Android的ImageButton的OnClick調用相同的LinearLayout另一個函數

這裏是XML:

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal"> 


      <ImageButton 
       android:id="@+id/yellow" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:scaleX="4" 
       android:scaleY="2.5" 
       android:layout_marginLeft="13dp" 
       android:layout_weight="1" 
       android:onClick="yellowPotted" 
       android:background="@drawable/ball_yellow"/> 

      <ImageButton 
       android:id="@+id/green" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:scaleX="4" 
       android:scaleY="2.5" 
       android:layout_marginLeft="13dp" 
       android:layout_weight="1" 
       android:onClick="greenPotted" 
       android:background="@drawable/ball_green"/> 

      <ImageButton 
       android:id="@+id/brown" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:scaleX="4" 
       android:scaleY="2.5" 
       android:layout_marginLeft="13dp" 
       android:layout_weight="1" 
       android:onClick="brownPotted" 
       android:background="@drawable/ball_brown"/> 

     </LinearLayout> 

這裏有我的活動3個功能(我不知道他們是否可能是有用的):

public void yellowPotted(View view) { 

    Log.d("BALLS", "YELLOW"); 

} 

public void greenPotted(View view) { 

    Log.d("BALLS", "GREEN"); 

} 

public void brownPotted(View view) { 

    Log.d("BALLS", "BROWN"); 

} 

使用setOnClickListener()我已經嘗試和覆蓋直接在新View.OnClickerListener()在我的活動代碼onClick()方法,但它不工作,要麼(完全相同的問題)。

任何人都知道可能是什麼問題?

+0

你的問題是什麼?錯誤?崩潰? –

+0

正如我寫的,前兩個ImageButtons調用錯誤的函數。我真的不知道什麼是錯的。 –

+0

你的ImageButtons是否重疊? –

回答

1

我試圖重現您的問題。我認爲它與你設定的比例有關。我用不同的尺度和不同的背景試用了你的代碼,它的工作原理。嘗試用不同比例的圖像。

+0

是的!尺度是問題!但是,我不知道他們爲什麼是這樣。你有這種行爲的解釋嗎? –

+1

@FedericoBacci因爲圖像重疊,並且android將視圖彼此疊加。所以,當你認爲你點擊第一個按鈕時,你實際上是點擊第二個按鈕,因爲它位於第一個按鈕的頂部 –

1

清理和重建項目。

是最有可能的是Android構建/緩存問題與ID的同時處理的資源。

+0

沒有。只是試過,沒有任何改變。 –

+0

有趣。代碼正在運行中?該佈局充滿了setContent()? –

+0

@skbrhmn發現問題 –

相關問題