2012-01-15 19 views

答えて

11

あなたのImageButtonがXMLで宣言されている場合は、単にもTextViewが含まれていLinearLayoutにそれを入れて、LinearLayoutonClickListenerを設定します。構造は、Javaで

<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> 

そしてようになります:

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

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

      } 
     }); 

Javaコードを経由して、動的に各ImageButtonを追加している場合、それはまだ同じ構造を維持します。何かを追加する必要があるかどうかを教えてください。

+0

の下に行うが、突然IMAGEBUTTONは、クリック後に動作しません。 .javaファイルにimagebuttonのonclicklistenerコードを保存して、画像ボタンとテキストの両方をクリック可能にする必要があります。 –

+2

あなたは間違っています。特定の項目を含むLinearLayoutがクリックをリッスンするように設定されている場合、画像もテキストも聴かれません。すべてのこれらのビューを含むレイアウトのみ。 – JoxTraex

+1

これは古い投稿ですが、上記のXiao Hanと同じ問題がありました(TextViewはクリック可能でしたがImageButtonはありませんでした)。私はImageButtonをImageViewに変更し、期待通りに機能しました。 – mjhouseman

0

あなたはこのリストを使用するGridViewの の王とGetViewメソッドでを作成したい場合はImageViewのとのTextViewでcustomlayout膨らままた

0

あなたは

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

のTextView

にonClickListener設定することができます

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とは、私は、それがのTextViewのクリック可能になることをしようとした

<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"/> 
関連する問題