2012-01-15 113 views

回答

11

如果你在XML中聲明你的ImageButton,那麼只要把它變成一個LinearLayout其中也包含了TextView並設置onClickListenerLinearLayout。該結構會像

<LinearLayout 
     ... > <!-- This is the LinearLayout for the xml document --> 

    <!-- Some other layout code here if you please --> 
    <LinearLayout 
     android:id="@+id/linLayout" 
      ... > 
     <ImageButton 
       ... /> 
     <TextView 
       ... /> 
    </LinearLayout> 
</LinearLayout> 

然後在你的java:

LinearLayout layout = (LinearLayout)findViewById(R.id.linLayout); 
     layout.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

      } 
     }); 

如果您正在添加的每個ImageButton通過動態java代碼,那麼它仍將保持相同的結構。讓我知道如果我需要添加任何東西。

+0

我試過了,這讓TextView的點擊,但突然的ImageButton不點擊後工作。我必須在.java文件中保留imagebutton的onclicklistener代碼,以使圖像按鈕和文本可點擊 – 2012-01-15 09:05:01

+2

然後你做錯了。如果包含特定項目的LinearLayout設置爲偵聽點擊,則圖像或文本都將被偵聽。只有包含所有這些視圖的佈局。 – JoxTraex 2012-01-15 14:08:33

+1

我知道這是一箇舊帖子,但是我和上面的小韓有同樣的問題(TextView是可點擊的,但ImageButton沒有)。我將ImageButton更改爲ImageView,並按預期工作。 – mjhouseman 2016-02-29 03:03:47

0

,如果你想創建列表使用的GridView 的王和getview方法膨脹customlayout用的ImageView和TextView的

0

您也可以設置onClickListener到TextView的

<!-- Some other layout code here if you please --> 
<LinearLayout 
    android:id="@+id/linLayout" 
     ... > 
    <ImageButton android:id="@+id/btn1" 
      ... /> 
    <TextView android:id="@+id/tv1" 
      ... /> 
</LinearLayout> 

java代碼:

ImageButton btn1 = (ImageButton) findViewById(R.id.btn1); 
btn1.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       myClick(); 

      } 
     }); 


TextView tv1 = (TextView) findViewById(R.id.tv1); 
tv1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       myClick(); 

      } 
     }); 
7

setAlpha並做以下

<Button 
     android:id="@+id/comparePack" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="5dp" 
     android:onClick="compareButtonClick" 
     android:text="@string/compare" 
     android:drawableTop="@drawable/compare1edit" 
     android:gravity="left|center_vertical"/> 
相關問題